[英]Adding attributes to all html tags
I want to add id="draggable"
attributes to all tags. 我想向所有标签添加
id="draggable"
属性。
For example - 例如 -
<img src="http://sample.jpg">
should change to <img src="http://sample.jpg" id="draggable" >
<img src="http://sample.jpg">
应更改为<img src="http://sample.jpg" id="draggable" >
<p>abcd</p>
should change to <p id="draggable">abcd</p>
<p>abcd</p>
应该更改为<p id="draggable">abcd</p>
This should apply to all tags except <HTML><HEAD><script><style>
这应该适用于除
<HTML><HEAD><script><style>
之外的所有标签
Any suggestion? 有什么建议吗?
Use class instead of Ids 使用类而不是ID
var elems = document.body.getElementsByTagName("*");
elems.setAttribute("class","draggable");
ID(s): Each element should only have one ID Each page can only have one element with that ID CLass(s): You can use mutliple classes on one element. ID:每个元素只能有一个ID每个页面只能有一个具有该ID的元素CLass:您可以在一个元素上使用多重类。 You can use the same class on multiple elements
您可以在多个元素上使用同一个类
Adding same id
to all the HTML elements is not a good way. 向所有HTML元素添加相同的
id
不是一个好方法。 Better to have same class name. 最好使用相同的类名。 Dynamically, if you want to add same class to all the elements, you can do something like below
动态地,如果要向所有元素添加相同的类,则可以执行以下操作
$('#parent').find('*').addClass('draggable');
http://jsfiddle.net/663m8qyd/ http://jsfiddle.net/663m8qyd/
<div id='parent'>
<p>ABC</p>
<img src='http://www.w3schools.com/tags/smiley.gif' alt="Smiley face"/>
<div id='child'>
<div id='child1'>test1</div>
<div id='child2'>test2</div>
</div>
</div>
To add draggable
class to all the elements inside parent
div, find each element and add respective class. 要将
draggable
类添加到parent
div内的所有元素,请找到每个元素并添加相应的类。 Hope that helps! 希望有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.