当前位置:网站首页>JS add custom attributes to elements

JS add custom attributes to elements

2022-06-25 15:52:00 Full stack programmer webmaster

Hello everyone , I meet you again , I'm your friend, Quan Jun .

Add custom attributes to elements
obj.setAttribute('attr_name','attr_value'); 
// for example obj.setAttribute('class','snow-container')
Add to element class Three methods of attribute
document.getElementsByTagName('body')[0].className = 'snow-container'; // Set as new 
document.getElementsByTagName('body')[0].className += 'snow-container'; // Add this after the original 
document.getElementsByTagName('body')[0].classList.add("snow-container"); // Equivalent to the first 

Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/152101.html Link to the original text :https://javaforall.cn

原网站

版权声明
本文为[Full stack programmer webmaster]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206251534383991.html