简体   繁体   English

如何使用jquery查找背景颜色为黄色的所有行?

[英]How to find all rows with background-color yellow using jquery?

I would like to find all rows with attribute background-color=rgb(255, 255, 0) . 我想找到属性为background-color=rgb(255, 255, 0)所有行。 Pressing on the button 'Search' should do this. 按“搜索”按钮可以执行此操作。 I don't understand why this is not working. 我不明白为什么这不起作用。 Nothing is found. 什么都没找到。 Code for searching: 搜索代码:

$("#btnSearch").click(function(){  
  var rows = $("#tbl1 tr[style='background-color:rgb(255, 255, 0)']");     
})

Whole example is here 整个例子就在这里

The clean approach would be to use a class instead of inline styles. 干净的方法是使用类而不是内联样式。

 $("#btnSearch").click(function() { console.log($('#tbl1 tr.yellow')) }) 
 .yellow { background-color: rgb(255, 255, 0); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table id='tbl1' class="table table-hover table-bordered"> <thead> <tr> <th>#</th> <th>Ime izdelka</th> <th>Opisni REF</th> <th>LOT/serijska/EDI</th> <th>Stanje (REF)</th> <th>Stanje (LOT)</th> <th>Privzeta skupina</th> </tr> </thead> <tfoot> </tfoot> <tbody> <tr class="yellow"> <td data-id="iROW_NUMBER" style="background-color: rgb(255, 255, 0);">1</td> <td data-id="cPROD_NME" style="background-color: rgb(255, 255, 0);">Kortikalni 2.0/14mm (zlati)</td> <td data-id="cPROD_DCD" style="background-color: rgb(255, 255, 0);">401.364</td> <td data-id="cPRSE_DCD" style="background-color: rgb(255, 255, 0);">8036572</td> <td data-id="iPROD_QUA_QUA" style="background-color: rgb(255, 255, 0);">6</td> <td data-id="cPRSS_QUA_QUA" style="background-color: rgb(255, 255, 0);">3</td> <td data-id="cPRGR_NME" style="background-color: rgb(255, 255, 0);">Stopalo - SYNTHES</td> </tr> <tr class="yellow"> <td data-id="iROW_NUMBER" style="background-color: rgb(255, 255, 0);">2</td> <td data-id="cPROD_NME" style="background-color: rgb(255, 255, 0);">Kortikalni 2.0/14mm (zlati)</td> <td data-id="cPROD_DCD" style="background-color: rgb(255, 255, 0);">401.364</td> <td data-id="cPRSE_DCD" style="background-color: rgb(255, 255, 0);">8327937</td> <td data-id="iPROD_QUA_QUA" style="background-color: rgb(255, 255, 0);">6</td> <td data-id="cPRSS_QUA_QUA" style="background-color: rgb(255, 255, 0);">1</td> <td data-id="cPRGR_NME" style="background-color: rgb(255, 255, 0);">Stopalo - SYNTHES</td> </tr> <tr> <td data-id="iROW_NUMBER">3</td> <td data-id="cPROD_NME">Kortikalni 2.0/14mm (zlati)</td> <td data-id="cPROD_DCD">401.364</td> <td data-id="cPRSE_DCD">9572333</td> <td data-id="iPROD_QUA_QUA">6</td> <td data-id="cPRSS_QUA_QUA">2</td> <td data-id="cPRGR_NME">Stopalo - SYNTHES</td> </tr> </tbody> </table> <br/> <br/> <button id="btnSearch">Search</button> 

Try this.. 尝试这个..

 $("#btnSearch").click(function(){ $("#tbl1 tr").each(function () { if($(this).css("background-color") == "rgb(255, 255, 0)") { alert('I am Yellow ;)'); } else{ alert('I am White ;)'); } }); }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table id='tbl1' class="table table-hover table-bordered"> <thead> <tr> <th>#</th> <th>Ime izdelka</th> <th>Opisni REF</th> <th>LOT/serijska/EDI</th> <th>Stanje (REF)</th> <th>Stanje (LOT)</th> <th>Privzeta skupina</th> </tr> </thead> <tfoot> </tfoot> <tbody> <tr style="background-color: rgb(255, 255, 0);"> <td data-id="iROW_NUMBER" style="background-color: rgb(255, 255, 0);">1</td> <td data-id="cPROD_NME" style="background-color: rgb(255, 255, 0);">Kortikalni 2.0/14mm (zlati)</td> <td data-id="cPROD_DCD" style="background-color: rgb(255, 255, 0);">401.364</td> <td data-id="cPRSE_DCD" style="background-color: rgb(255, 255, 0);">8036572</td> <td data-id="iPROD_QUA_QUA" style="background-color: rgb(255, 255, 0);">6</td> <td data-id="cPRSS_QUA_QUA" style="background-color: rgb(255, 255, 0);">3</td> <td data-id="cPRGR_NME" style="background-color: rgb(255, 255, 0);">Stopalo - SYNTHES</td> </tr> <tr style="background-color: rgb(255, 255, 0);"> <td data-id="iROW_NUMBER" style="background-color: rgb(255, 255, 0);">2</td> <td data-id="cPROD_NME" style="background-color: rgb(255, 255, 0);">Kortikalni 2.0/14mm (zlati)</td> <td data-id="cPROD_DCD" style="background-color: rgb(255, 255, 0);">401.364</td> <td data-id="cPRSE_DCD" style="background-color: rgb(255, 255, 0);">8327937</td> <td data-id="iPROD_QUA_QUA" style="background-color: rgb(255, 255, 0);">6</td> <td data-id="cPRSS_QUA_QUA" style="background-color: rgb(255, 255, 0);">1</td> <td data-id="cPRGR_NME" style="background-color: rgb(255, 255, 0);">Stopalo - SYNTHES</td> </tr> <tr> <td data-id="iROW_NUMBER">3</td> <td data-id="cPROD_NME">Kortikalni 2.0/14mm (zlati)</td> <td data-id="cPROD_DCD">401.364</td> <td data-id="cPRSE_DCD">9572333</td> <td data-id="iPROD_QUA_QUA">6</td> <td data-id="cPRSS_QUA_QUA">2</td> <td data-id="cPRGR_NME">Stopalo - SYNTHES</td> </tr> </tbody> </table> <br/> <br/> <button id="btnSearch">Search</button> 

The characters string you use should be the same on the jQuery and the HTML. 您使用的字符串在jQuery和HTML上应该是相同的。

In your jQuery you forgot the semicolon ; 在你的jQuery中你忘记了分号 ; and a space 和一个空间 after the : . 之后:

Try this string. 试试这个字符串。

#tbl1 tr[style='background-color: rgb(255, 255, 0);']

Anyway, I dont recomend you to use inline styles this way. 无论如何,我不建议你这样使用内联样式。

Use jQuery .filter() to filtering selected tr and select only tr has target background color. 使用jQuery .filter()过滤选定的tr并仅选择tr具有目标背景颜色。 In function callback get background-color of tr using this.style.backgroundColor and check that is equal to rgb(255, 255, 0) or not. 在函数回调中使用this.style.backgroundColor获取tr的background-color ,并检查是否等于rgb(255, 255, 0)

$("#btnSearch").click(function(){
  var trs = $("#tbl1 tr").filter(function(){
    return this.style.backgroundColor == "rgb(255, 255, 0)";
  });
});

Note that in the bottom example after click, color of target tr changed to red . 请注意,在单击后的底部示例中,目标tr颜色变为red

 $("#btnSearch").click(function(){ $("#tbl1 tr").filter(function(){ return this.style.backgroundColor == "rgb(255, 255, 0)"; }).css("color", "red"); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table id='tbl1' class="table table-hover table-bordered"> <tr style="background-color: rgb(255, 255, 0);"> <td>1</td><td>2</td><td>3</td> </tr> <tr style="background-color: rgb(255, 255, 255);"> <td>1</td><td>2</td><td>3</td> </tr> <tr style="background-color: rgb(0, 0, 0);"> <td>1</td><td>2</td><td>3</td> </tr> <tr style="background-color: rgb(255, 255, 0);"> <td>1</td><td>2</td><td>3</td> </tr> <tr> <td>1</td><td>2</td><td>3</td> </tr> </table> <button id="btnSearch">Search</button> 

Check result of code on full html in demo 演示中检查完整html上的代码结果

Try this; 尝试这个;

$('#tbl1 tr').filter(function() {
     return $(this).css('background-color') == 'rgb(255, 255, 0)';
})

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

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