在脚本标签中使用ID属性的要点是什么? [英] What is the point of using an ID attribute in a script tag?

查看:63
本文介绍了在脚本标签中使用ID属性的要点是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我在问什么是给我的脚本标签一个id属性的意义?由于这种识别,它们中的例程可以被调用或引用不同吗?这可能会导致任何问题使脚本/页面变得有趣吗?

Basically what I am asking is what is the point of giving my script tags an id attribute? Can the routines inside of them be called or referenced differently because of this identification? Could this cause any problems making the script/page act funny?

谢谢。

推荐答案

id 只是DOM树中< script> 标签的另一个访问者。您可以理论上使用 document.getElementById()来检索< script> 节点并将其删除或添加其他属性(虽然我不相信你可以修改 src 属性,一旦它被加载到DOM中)。这些操作不需要 id - 它可能已被任何DOM函数访问,例如 getElementsByTagName(script)

The id is just another accessor of the <script> tag inside the DOM tree. You could in theory use document.getElementById() to retrieve the <script> node and delete it or add other attributes (though I don't believe you can modify the src attribute once it has loaded in the DOM). The id isn't required for those operations though -- it could have been accessed by any DOM function such as getElementsByTagName("script") as well.

如果您确实需要使用DOM访问< script> 标签操纵, id 使它更容易一些。否则,没有什么好处 1

If you do need to access the <script> tag with DOM manipulations, the id makes it just a little easier. Otherwise, there is little benefit1.

1 这样添加一个 / code>属性到任何 DOM节点,尽管影响演示的节点也可以从针对 id 的CSS中获益,而不像< script> 标签...

1That is sort of true of adding an id attribute to any DOM node, though nodes affecting presentation can also benefit from CSS targeting the id, unlike a <script> tag...

这篇关于在脚本标签中使用ID属性的要点是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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