简体   繁体   English

jquery find() 等效于 javascript

[英]jquery find() equivalent for javascript

I have three tables and i want to check wheter they contain a specific element, eg a button with the value 'Previous'.我有三个表,我想检查它们是否包含特定元素,例如一个值为“上一个”的按钮。 I solved it by using the jquery function find and writing a function, but i need to solve this problem without jquery.我通过使用jquery函数find并编写一个函数来解决它,但是我需要在没有jquery的情况下解决这个问题。 Is this possible?这可能吗?

 var t1 = document.getElementById("table_one"); var t2 = document.getElementById("table_two"); var t3 = document.getElementById("table_three"); has_prev_button(t1); has_prev_button(t2); has_prev_button(t3); function has_prev_button(element) { var has_prev_button = false; var check = $(element).find("input[type=button]"); for (i=0; i<=check.length-1; i++) { if (check[i].getAttribute("value") == "Previous") { has_prev_button = true; } } if (has_prev_button) { document.write("<p>The selected table has a Previous button</p>"); } else { document.write("<strong><p style='color:red'>The selected table has NO Previous button</p></strong>"); } }
 table { margin-bottom:40px; border: 1px solid black; } td { border: 2px solid #D8D8D8; width: 70px; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table id="table_one"> <tr> <td>Foo</td> <td>Bar</td> <tr> <tr> <td><input type="button" value="Previous"></td> <td><input type="button" value="Next"></td> </tr> </table> <table id="table_two"> <tr> <td>Foo</td> <td>Bar</td> <tr> <tr> <td></td> <td><input type="button" value="Next"></td> </tr> </table> <table id="table_three"> <tr> <td>Foo</td> <td>Bar</td> <tr> <tr> <td><input type="button" value="Previous"></td> <td><input type="button" value="Next"></td> </tr> </table>

Use element.querySelectorAll :使用element.querySelectorAll

 var t1 = document.getElementById("table_one"); var t2 = document.getElementById("table_two"); var t3 = document.getElementById("table_three"); has_prev_button(t1); has_prev_button(t2); has_prev_button(t3); function has_prev_button(element) { var has_prev_button = false; var check = element.querySelectorAll("input[type=button]"); for (i=0; i<=check.length-1; i++) { if (check[i].value == "Previous") { has_prev_button = true; } } if (has_prev_button) { document.write("<p>The selected table has a Previous button</p>"); } else { document.write("<strong><p style='color:red'>The selected table has NO Previous button</p></strong>"); } }
 table { margin-bottom:40px; border: 1px solid black; } td { border: 2px solid #D8D8D8; width: 70px; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table id="table_one"> <tr> <td>Foo</td> <td>Bar</td> <tr> <tr> <td><input type="button" value="Previous"></td> <td><input type="button" value="Next"></td> </tr> </table> <table id="table_two"> <tr> <td>Foo</td> <td>Bar</td> <tr> <tr> <td></td> <td><input type="button" value="Next"></td> </tr> </table> <table id="table_three"> <tr> <td>Foo</td> <td>Bar</td> <tr> <tr> <td><input type="button" value="Previous"></td> <td><input type="button" value="Next"></td> </tr> </table>

Simply use document.querySelectorAll along with the classes spaced apart .只需将document.querySelectorAll间隔开一起使用。

For example, if I want to find the button in the 3rd section and change its background color :例如,如果我想在第三部分中找到按钮并更改其背景颜色

jQuery jQuery

$('.my_sections').eq(2).find('.my_button').css('background', 'pink')

Vanilla JS香草JS

document.querySelectorAll('.my_sections .my_button')[2].style.background = 'pink'

Similarly, If I wanted to check how many buttons I had of the my_button class:同样,如果我想检查my_button 类有多少个按钮

document.querySelectorAll('.my_sections .my_button').length

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

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