简体   繁体   English

单击时更改跨度的颜色

[英]Change color of span upon click

https://codepen.io/SmileySteven/pen/pdBYdv?editors=1111 https://codepen.io/SmileySteven/pen/pdBYdv?editors=1111

function test(){
  var span = document.querySelectorAll('span');
  for(var i = 0 ; i <= span.length ;i++){
    span.addEventListener('click', function(){
    this.style.backgroundColor === '#ffffff'? this.style.backgroundColor = 'yellow'
         : this.style.backgroundColor="#000000";                          
   })
 }
}
test();

Hello guys, I am trying to change the background color of my spans upon click but it does not seem to work - care to guide me as to what I am doing wrong? 大家好,我试图改变我的跨度的背景颜色点击但它似乎没有工作 - 关心指导我做错了什么? I have set all span to background color of #ffffff 我已将所有范围设置为#ffffff背景颜色

span.addEventListener('click', function(){

You are setting the event to whole collection of spans. 您将事件设置为整个跨度集合。 You should set to the individual element 您应该设置为单个元素

span[i].addEventListener('click', function(){

https://codepen.io/anon/pen/LOvamO?editors=1111 https://codepen.io/anon/pen/LOvamO?editors=1111

 function test(){ var span = document.querySelectorAll('span'); span.forEach((item)=>{ item.addEventListener('click',()=>{ let styles = getComputedStyle(item); styles.backgroundColor == 'rgb(255, 255, 255)' ? item.style.backgroundColor = 'yellow' : item.style.backgroundColor = 'black'; }); }); } test(); 
 .def.def2{ background-color:orange } div#ya{ background-color:purple } span{ background-color:#ffffff } 
 <input type="text" id="i1"> </input> <input type="text" id="i2"> </input> <input type="number" id="i3"> </input> <button id="btn"> sad </button> <div> Div1 </div> <div class="def def2"> div2 </div> <div id="ya" class="def def2 def3 omg"> div2 </div> <p> sad </p> <span > test </span><span> test </span><span> test </span> 

I recommend to use forEach with querySelectorAll() . 我建议在querySelectorAll()使用forEach

And, if you want to get the value of background-color , you should better use getComputedStyle() . 而且,如果你想获得background-color的值,你最好使用getComputedStyle()

But, getComputedStyle().backgroundColor always return rgb() , so you have to use rgb() . 但是, getComputedStyle().backgroundColor总是返回rgb() ,所以你必须使用rgb()

1 - JavaScript always garbing White backgrounds From HTML as null for Printing purpose . 1 - JavaScript总是装饰白色背景从HTML为空,用于打印目的。

so in case the background you trying to match is white 所以如果你想要匹配的背景是白色的
then replace : 然后替换

this.style.backgroundColor === '#ffffff' ? ..... etc ;

with this : 有了这个 :

this.style.backgroundColor === "" ? this.style.backgroundColor = 'yellow'
     : this.style.backgroundColor="#000000";

otherways if the background your trying to match is Not white 另外,如果您尝试匹配的背景不是白色
then you can use the condition normal element === " color code here " 那么你可以使用条件法线element === " color code here "

2 - finally as others mention add the loop in front of your span callback like this : 2 -最后正如其他人提到的那样在你的span回调之前添加循环,如下所示:

span[i].addEventListener('click', function(){ ... etc

Adding onto the above answer, I would also recommend running test on document ready. 除了上面的答案,我还建议在文件就绪时运行测试。

$( document ).ready(test);

Updated codepen with fix: https://codepen.io/TheRodeo/pen/pdBYVJ 使用修复程序更新了codepen: https ://codepen.io/TheRodeo/pen/pdBYVJ

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

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