javascript必须在head标签中吗? [英] Does javascript have to be in the head tags?

查看:181
本文介绍了javascript必须在head标签中吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相信javascript可以在任何地方(差不多),但我几乎总是在< head>< / head> 之间看到它。我正在使用jquery,并想知道它是否由于某种原因必须在head标签中,或者如果我移动它会破坏某些东西。谢谢。

I believe javascript can be anywhere (almost), but I almost always see it in between <head></head>. I am using jquery and wanted to know if it has to be in the head tags for some reason or if will break something if I move it. Thank you.

编辑:为什么它几乎总是在head标签中?

Why is it almost always in the head tags?

推荐答案

JavaScript在文档中的任何位置执行。如果您将内联JavaScript放在正文中,它将在浏览器到达时执行。如果您使用 $(document).ready(...)来执行操作,那么定位无关紧要。否则,您可能会发现重要的情况。一般来说,没关系。脚本最终出现在 head 标签中,大部分都不符合传统。

JavaScript is executed wherever it is found in the document. If you place inline JavaScript in the body, it will be executed when the browser comes to it. If you're using $(document).ready(...) to execute things, then the positioning shouldn't matter. Otherwise, you may find corner cases where it matters. In general, it does not matter. Scripts end up in the head tag mostly out of tradition.

这篇关于javascript必须在head标签中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆