如何使用javascript(No JQ)查找和替换类(How to Find and replace class with javascript (No JQ))
例如:我有<div class="oneClass twoClass colorRed">Content</div> ,我想找到color*类,并将颜色部分“Red”替换为新值“Blue”或“Yellow”,例如所以现在它将是“ colorBlue ”......
我如何只用Javascript做到这一点?
谢谢!
更新:这不是我的问题: 使用JavaScript更改元素的类
For example: i have <div class="oneClass twoClass colorRed">Content</div> and i want to find the color* class and replace the color part "Red" with the new value "Blue" or "Yellow" for example so it would be "colorBlue" now...
How do i do that with Javascript only ?
Thanks!
Update: This isn't my question: Change an element's class with JavaScript
最满意答案
var node = document.getElementById('someDiv'); node.className = node.className.replace(/color.*/, 'colorBlue');/* This css snippet will allow to see * element's classes rendered with the html ;)*/ div:after { content: attr(class); margin-left: 5px; font-style: italic; color: gray; }<div id="someDiv" class="oneClass twoClass colorRed">Content</div>var node = document.getElementById('someDiv'); node.className = node.className.replace(/color.*/, 'colorBlue');/* This css snippet will allow to see * element's classes rendered with the html ;)*/ div:after { content: attr(class); margin-left: 5px; font-style: italic; color: gray; }<div id="someDiv" class="oneClass twoClass colorRed">Content</div>
更多推荐
发布评论