代码之家  ›  专栏  ›  技术社区  ›  Abilash Erikson

选择背景色不在jQuery中的元素

  •  5
  • Abilash Erikson  · 技术社区  · 9 年前

    https://jsfiddle.net/oL7tdL22/1/

    $(function(){
    
    $(".testing").each(function(){
       if($(this).css("background-color")=="rgb(255,193,0)"){
         alert("found");
       }
       else{
         alert("not found");
       }
      });
    
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <div class="parent">
    <div class="testing"  style="background-color:rgb(255,193,0)">
    Test
    </div>
    </div>
    
    <div class="parent">
    <div class="testing"  style="background-color:rgb(220, 4, 81)">
    Test
    </div>
    </div>
    
    
    <div class="parent">
    <div class="testing"  style="background-color:rgb(0, 186, 76)">
    Test
    </div>
    </div>

    当我们处于警戒背景色时,它就起作用了。但是我们无法匹配颜色。

    2 回复  |  直到 9 年前
        1
  •  5
  •   Dij    9 年前

    在rgb颜色代码中,需要在每个逗号后留出一个空格,如 rgb(255, 193, 0)

    $(function(){
    
    $(".testing").each(function(){
       if($(this).css("background-color")=="rgb(255, 193, 0)"){
         alert("found");
       }
       else{
         alert("not found");
       }
      });
    
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <div class="parent">
    <div class="testing"  style="background-color:rgb(255,193,0)">
    Test
    </div>
    </div>
    
    <div class="parent">
    <div class="testing"  style="background-color:rgb(220, 4, 81)">
    Test
    </div>
    </div>
    
    
    <div class="parent">
    <div class="testing"  style="background-color:rgb(0, 186, 76)">
    Test
    </div>
    </div>
        2
  •  3
  •   Koby Douek Cicero Silva Luiz Junior    9 年前

    rgb 当JQuery解析 .css("background-color") 以这种方式估价。

    rgb(X, Y, Z)
          ▲  ▲
    

    这是正确的代码:

    if($(this).css("background-color")=="rgb(255, 193, 0)"){
       alert("found");
    }
    

    $(function(){
         $(".testing").each(function(){
            if($(this).css("background-color")=="rgb(255, 193, 0)")
              alert("found");
            else
              alert("not found");
         });
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <div class="parent">
        <div class="testing"  style="background-color:rgb(255,193,0)">Test</div>
    </div>
    <div class="parent">
        <div class="testing"  style="background-color:rgb(220, 4, 81)">Test</div>
    </div>
    <div class="parent">
        <div class="testing"  style="background-color:rgb(0, 186, 76)">Test</div>
    </div>