简体   繁体   English

如何仅选择具有特定属性的元素?

[英]How can I select an element with specific attributes only?

I need to paste src to image depending on color choice. 我需要根据颜色选择将src粘贴到图像。

Here is my markup: 这是我的标记:

<div data-img="[black-blue.png]" black blue></div>
<div data-img="[black.png]"  black></div>
<div data-img="[blue.png]"  blue></div>
<div data-img="[orange.png]"  orange></div>
<div data-img="[black-blue-orange.png]" black blue orange></div>
<div data-img="[blue-orange.png]" blue orange></div>
<div data-img="[blue-red-orange.png]" blue red orange></div>

My script works on elements attributes: 我的脚本适用于元素属性:

var attributes = '';
for(var i = 0; i<activeColorsArray.length; i++ ){
   attributes += '['+activeColorsArray[i]+']';
}

Output: [blue][black] , for examples. 输出: [blue][black] ,例如。 It's works for black&blue item, but it's catch black&blue&orange element too. 它适用于黑色和蓝色项目,但它也捕捉黑色和蓝色和橙色元素。

[blue][black]:not([orange]) not working for me because I have 8 colors... [blue][black]:not([orange])不适合我,因为我有8种颜色......

Need something like ONLY THIS selector. 需要像这个选择器一样的东西。

One idea is to consider the number of attributes an element has. 一个想法是考虑元素具有的属性数量。 If you want to target only blue and black then you target the elements with only 3 attributes (including data-img in your case) 如果您只想定位blueblack那么您只使用3个属性(包括您的情况下的data-img )来定位元素

 activeColorsArray= ['blue','black']; var attributes = ''; for(var i = 0; i<activeColorsArray.length; i++ ){ attributes += '['+activeColorsArray[i]+']'; } var elements = document.querySelectorAll(attributes); var items = Array.from(elements); elements = items.filter((e) => { return e.attributes.length == 3; }) for(var i=0;i<elements.length;i++) { elements[i].style.color="red"; } 
 <div data-img="[black-blue.png]" black blue>black blue</div> <div data-img="[black.png]" black>black</div> <div data-img="[blue.png]" blue>blue</div> <div data-img="[orange.png]" orange>orange</div> <div data-img="[black-blue-orange.png]" black blue orange>black blue orange</div> <div data-img="[blue-orange.png]" blue orange>blue orange</div> <div data-img="[blue-red-orange.png]" blue red orange>blue red orange</div> 

If that's your exact markup, the CSS attribute selector would work without any JavaScript. 如果这是您的确切标记,CSS属性选择器将在没有任何JavaScript的情况下工作。 This code will match only divs with the data-img attribute with a value of exactly [black-blue.png] . 此代码仅匹配具有data-img属性的div,其值为[black-blue.png] Trying to select by naked attributes ( div[black][blue] {} ) is not recommended for the exact reason you've described. 由于您所描述的确切原因,不建议尝试使用裸属性( div[black][blue] {} )进行选择。

 div[data-img="[black-blue.png]"] { color: red; } 
 <div data-img="[black-blue.png]" black blue>black blue</div> <div data-img="[black.png]" black>black</div> <div data-img="[blue.png]" blue>blue</div> <div data-img="[orange.png]" orange>orange</div> <div data-img="[black-blue-orange.png]" black blue orange>black blue orange</div> <div data-img="[blue-orange.png]" blue orange>blue orange</div> <div data-img="[blue-red-orange.png]" blue red orange>blue red orange</div> 

https://jsfiddle.net/nk98tch1/ https://jsfiddle.net/nk98tch1/

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

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