带有ID的脚本标签? [英] Script tags with IDs?

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

问题描述

大多数现代浏览器是否支持脚本标签中的ID,例如:

Do most modern browsers support ids in script tags such as:

<script id="aParticularScript">/* ... */</script>

我问的原因是Eclipse显示警告,指出未定义的属性名称",但是当我使用jQuery选择器获取脚本元素的其他属性时,它在Google Chrome中运行良好.W3Schools指出script元素不支持任何标准属性(包括id属性),但是我学会了不信任W3Schools.

The reason I ask is that Eclipse displays a warning stating "undefined attribute name" but it works fine in Google Chrome when I use jQuery selectors to get other properties of the script element. W3Schools states that the script element does not support any standard attributes (including the id attribute), but I've learned not to trust W3Schools.

可以让script标签有一个id吗?

Is it okay to have the script tag have an id?

推荐答案

HTML 4答案:

不,您不能,至少如果想要有效的HTML则不能...

No, you can't, at least not if you want valid HTML...

以下元素不能具有ID属性:

The following elements can't have an ID attribute:

  • < base>
  • < head>
  • < html>
  • < meta>
  • < script>
  • < style>
  • < title>
  • <base>
  • <head>
  • <html>
  • <meta>
  • <script>
  • <style>
  • <title>

可能还有更多.

这令人困惑,因为仅查看ID/class属性的文档并没有明确说明它们不能与这些元素一起使用.您必须查看元素的DTD才能看到未为元素定义常规属性,因此无法使用.

This is confusing because viewing just the documentation for the ID/class attributes doesn't specifically say that they can't be used with these elements. You have to look at the DTD for the elements to see that the general attributes are not defined for the element, and thus cannot be used.

HTML 5答案:

默认文档类型声明 HTMLElement ,适用于HTML中的所有元素,指定这些

The default document type declaration, HTMLElement, which applies to all elements in HTML specifies that these global attributes (including the ID attribute) can be used on any element you create, so it would appear you can do this in HTML 5, but not in HTML 4.

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

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