简体   繁体   English

循环遍历具有特定样式的所有元素

[英]Loop through all elements with a specific style

If I have a bunch of elements on the page with background-color: #000685;如果我在页面上有一堆带有background-color: #000685; that have nothing in common (no common ids, classes, tag names etc,.).没有共同点(没有共同的 id、类、标签名称等)。

Is there a way to loop through them all with JavaScript and change the background color value?有没有办法用 JavaScript 遍历它们并更改背景颜色值?

Since the elements have nothing in common, the best you can do is loop through every element on the page, get the background color for each one, and then perform checks.由于这些元素没有任何共同点,您能做的最好的事情就是遍历页面上的每个元素,获取每个元素的背景颜色,然后执行检查。 Like so:像这样:

 var elements = document.getElementsByTagName("*"); for(let i = 0; i < elements.length; i++){ var cur = elements[i]; if(cur.style.backgroundColor=="rgb(0, 6, 133)"){ cur.style.backgroundColor="red"; } }
 <div style="background-color: #000685;">I should be red</div> <div>I should not be red.</div> <div style="background-color: #000685;">I should be red</div> <div style="background-color: #000685;">I should be red</div> <div>I should not be red.</div> <div>I should not be red.</div> <p style="background-color: #000685;">I should be red</p> <p>I should not be red.</p>

If the style isn't inline, use getComputedStyle().getPropertyValue("background-color") :如果样式不是内联样式,请使用getComputedStyle().getPropertyValue("background-color")

 var elements = document.getElementsByTagName("*"); for(let i = 0; i < elements.length; i++){ var cur = elements[i]; if(window.getComputedStyle(cur).getPropertyValue("background-color")=="rgb(0, 6, 133)"){ cur.style.backgroundColor="red"; } }
 .one{ background-color: #000685; }.two{ background-color: #000685; }.three{ background-color:black; color:white; }
 <div class="one">I should be red</div> <div>I should not be red.</div> <div class="two">I should be red</div> <div class="one">I should be red</div> <div class="three">I should not be red.</div> <div>I should not be red.</div> <p class="two">I should be red</p> <p class="three">I should not be red.</p>

You can add some attribute to make the targeting proccess easier:您可以添加一些属性以使定位过程更容易:

<span 
    style="background-color: #FFFFFF;" 
    background-color
>
    Lorem ipsum dolor sit amet
</span>
<span>Lorem ipsum dolor sit amet</span>
<span 
    style="background-color: #FFFFFF;" 
    background-color
>
    Lorem ipsum dolor sit amet
</span>
<div>Lorem ipsum dolor sit amet</div>
<span 
    style="background-color: #FFFFFF;" 
    background-color
>
    Lorem ipsum dolor sit amet
</span>
<div>Lorem ipsum dolor sit amet</div>
<p 
    style="background-color: #FFFFFF;" 
    background-color
>
    Lorem ipsum dolor sit amet
</p>
<span 
    style="background-color: #FFFFFF;" 
    background-color
>
    Lorem ipsum dolor sit amet
</span>

js: js:

console.log(document.querySelectorAll('[background-color]'));

output: output:

// [object NodeList] (5)
["<span/>","<span/>","<span/>","<p/>","<span/>"] 

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

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