Html 给脚本标签一个 ID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2741441/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Giving the script tag an ID
提问by The Code Pimp
I came across a scenario where giving a scriptelement an idattribute would solve a problem easily. However, after reading about the scriptelement at w3schoolsand quirksmode, it seems doing so could have some unforeseen consequences.
我遇到了一个场景,给script元素一个id属性可以轻松解决问题。但是,在阅读了w3schools和quirksmode 中的script元素后,似乎这样做可能会产生一些不可预见的后果。
Has anyone come across any of these issues with browsers such as Chrome, Safari, FF3 up and IE 7 up?
有没有人在 Chrome、Safari、FF3 up 和 IE 7 up 等浏览器上遇到过这些问题?
回答by bobince
It's fine in all current browsers.
在所有当前浏览器中都很好。
The only browser that got <script id>wrong was Netscape 4, which we stopped caring about a long, long time ago.
唯一<script id>出错的浏览器是 Netscape 4,我们很久很久以前就不再关心它了。
That quirksmode page seems to be badly out of date, what with its use of languageattributes, script <!--hiding, and application/x-javascript. Its advice about avoiding <script>in the <body>(and putting it in <head>instead) is at odds with today's encouraged practices.
那个 quirksmode 页面似乎已经过时了,它使用了language属性、脚本<!--隐藏和application/x-javascript. 它如何避免建议<script>在<body>(并把它在<head>代替)在今天的鼓励的做法不一致。
If we're talking <script>attribute compatibility problems: deferdoesn't work everywhere so don't rely on it; charsetdoesn't work everywhere, and neither does the charset parameter on the served script's Content-Type, so your script charset had better match the page; typeshould always be text/javascriptand not one of the non-working alternatives the pedants who wrote RFC 4329 would like you to use.
如果我们谈论的是<script>属性兼容性问题:defer并非在任何地方都有效,所以不要依赖它;charset并非在任何地方都有效,所提供脚本的 Content-Type 上的字符集参数也无效,因此您的脚本字符集最好与页面匹配;type应该始终是text/javascript编写 RFC 4329 的书呆子希望您使用的非工作替代方案之一。
回答by niutech
回答by Robusto
If you're still having to support Netscape 4, you've got a lot of trouble – and the pity and condolences of the rest of the developer world.
如果您仍然必须支持 Netscape 4,那么您就会遇到很多麻烦——以及其他开发人员世界的同情和哀悼。
Short answer, I wouldn't worry about it.
简短的回答,我不会担心。
回答by JBourne
I know a long time has passed, but I thought it would be nice to point that when you look at W3 Schools definition of the script tag, you see at some point that
我知道很长一段时间已经过去了,但我认为当您查看W3 Schools 对 script 标签的定义时,您会在某些时候看到这一点会很好
The tag also supports the Global Attributes in HTML.
and amongst those attributes, ta-da, you will find your lovely id.
在这些属性中,ta-da,你会找到你可爱的id。
The same goes for a whole lot of tags, which will certainly give us a lot of flexibility when pulling those nice trick from the hat.
很多标签也是如此,当从帽子里拿出那些漂亮的技巧时,这肯定会给我们很大的灵活性。
回答by alex
I don't think a browser would have a problem by adding an idattribute to a scriptelement.
我认为浏览器通过向元素添加id属性不会有问题script。
On some of my sites, that load additional JavaScripts via JavaScript, I have added a classattribute to make referencing them easier. The validator did not complain.
在我的一些网站上,通过 JavaScript 加载额外的 JavaScript,我添加了一个class属性来使引用它们更容易。验证器没有抱怨。


