简体   繁体   English

替换 html 元素 bu 类的样式属性

[英]Replacing style attribute of a html element bu class

I want to replace the style attribute of element.我想替换元素的样式属性。

<a class="label-anchor" href="link"><span class="label" style="border-color:#color; color:#color;"></span></a>

i want that the color attribute get replaced by background with same color using js.我希望使用 js 将颜色属性替换为具有相同颜色的背景 Please help...请帮忙...

You can use querySelector to get the element by its class.您可以使用 querySelector 按其类获取元素。

const element = document.querySelector('.label');
element.style.removeProperty('color');
element.style.backgroundColor = 'what you want';

Try this:尝试这个:

var elements = document.getElementsByClassName('label');
for(var i = 0; i < elements.length; i++){
    elements[i].style.background = elements[i].style.color;
    elements[i].style.color = null;  
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM