本文介绍了为脚本标记一个ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个场景,其中给出脚本元素和 id 属性可以轻松解决问题。然而,阅读和,它似乎这样做可能会带来一些不可预见的后果。



有没有人遇到过Chrome,Safari,FF3和IE 7等浏览器出现这些问题?

解决方案

在当前所有浏览器中都很好。


$ b

c $ c>< script id> 错误的是Netscape 4,我们在很久很久以前就停止关注它。



那quirksmode页面似乎严重过时,使用语言属性,脚本<! - 隐藏,和 application / x-javascript 。其建议避免< body> 中的< script> (并将其放入< head> )与今天鼓励的做法不一致。

如果我们正在谈论<脚本> 属性兼容性问题: defer 无处不在,所以不要依赖它; charset 在任何地方都无法正常工作,并且所提供脚本的Content-Type上的charset参数也没有,因此脚本charset与页面匹配得更好; type 应始终为 text / javascript ,而不是编写RFC 4329你使用。


I came across a scenario where giving a script element an id attribute would solve a problem easily. However, after reading about the script element at w3schools and quirksmode, it seems doing so could have some unforeseen consequences.

Has anyone come across any of these issues with browsers such as Chrome, Safari, FF3 up and IE 7 up?

解决方案

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.

That quirksmode page seems to be badly out of date, what with its use of language attributes, 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.

If we're talking <script> attribute compatibility problems: defer doesn't work everywhere so don't rely on it; charset doesn'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; type should always be text/javascript and not one of the non-working alternatives the pedants who wrote RFC 4329 would like you to use.

这篇关于为脚本标记一个ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 11:46
查看更多