简体   繁体   English

CSS [attribute = value]选择器未应用

[英]CSS [attribute=value] selector does not get applied

I have a div . 我有一个div I want it to change color between red and green when clicked. 我希望它在单击时能在红色和绿色之间改变颜色。

I wrote two working functions in JS to accomplish this. 我在JS中编写了两个工作函数来完成此任务。
One called works just changes the color. 一种叫做works只是改变颜色。
One called doesntWork negates attribute clicked , and depends on CSS to change the color, which doesn't happen. 一种叫做doesntWork属性clicked ,它依靠CSS来改变颜色,这种情况不会发生。 Why? 为什么? And how can I fix this? 我该如何解决呢?

 function doesntWork(div) { div.clicked = !div.clicked console.log(div.clicked) } function works(div) { // console.log(div.style['background-color']) if(div.style['background-color'] === "red"){ div.style['background-color'] = "green" } else { div.style['background-color'] = "red" } } 
 div { background: green; width: 100px; height: 100px; margin: 20px; display: inline-block; border-radius: 1em; } div[clicked="true"] { background: red; } 
 <div onclick="works(this)"></div> <div onclick="doesntWork(this)"></div> 

You're not setting an attribute clicked but a property . 您不是在设置clicked 属性,而是在设置属性 The code below demonstrates how to use setAttribute and getAttribute correctly. 下面的代码演示了如何正确使用setAttributegetAttribute Also, for custom properties you should use the data- prefix. 另外,对于自定义属性,您应该使用data-前缀。

You need to use the ternary condition, instead of simply negating it: on the first click, the attribute is null . 您需要使用三元条件,而不是简单地否定它:第一次单击时,该属性为null Negating it yields true , which is then set as an attribute and converted to a string 'true' . 否定它会产生true ,然后将其设置为属性并转换为字符串'true' Negating this a second time will make it false'false' . 再次否定此参数将使其为false'false' Not because it was 'true' , but because it was a non-empty string ! 不是因为它是'true' ,而是因为它是一个非空字符串 Any further click will keep the attribute value at 'false' . 再次单击将使属性值保持为'false'

Also, because the attribute is neither 'true' nor 'false' at the first time, this is checking whether it's not 'true' . 另外,由于该属性在第一次时既不是'true'也不是'false' ,因此这是在检查它是否不是 'true'

 function doesntWork(div) { div.setAttribute('data-clicked',div.getAttribute('data-clicked')!='true'?'true':'false'); console.log(div.getAttribute('data-clicked')); } function works(div) { // console.log(div.style['background-color']) if(div.style['background-color'] === "red"){ div.style['background-color'] = "green"; } else { div.style['background-color'] = "red"; } } 
 div { background: green; width: 100px; height: 100px; margin: 20px; display: inline-block; border-radius: 1em; } div[data-clicked="true"] { background: red; } 
 <div onclick="works(this)"></div> <div onclick="doesntWork(this)"></div> 

This function don't work, because there aren't clicked attribute in div specifications . 此功能无效,因为div 规范中没有clicked属性。 This type of selector you can use just for attributes, you can't create your own. 您只能将此类选择器用于属性,而不能创建自己的选择器。

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

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