How to create and insert an attribute node

The following two methods may be used to create a new property node provided to the node specific elements.

var node = document.getElementById('div1');

var a = document.createAttribute('my_attrib');
a.value = 'newVal';

node.setAttributeNode(a);
// 或者
node.setAttribute('my_attrib', 'newVal');

 

Note: The attribute names and values ​​here can be customized.

Guess you like

Origin www.cnblogs.com/aisowe/p/11546395.html