开发者

JavaScript querySelectorAll

开发者 https://www.devze.com 2023-02-14 05:33 出处:网络
A tutorial I\'m working on has the following code with the following comment. I don\'t understand... i) the comment, specifically, why it says \"all second table cells.\"What does \"second\" mean? Th

A tutorial I'm working on has the following code with the following comment. I don't understand...

i) the comment, specifically, why it says "all second table cells." What does "second" mean? This isn`t proper English

ii) what is 开发者_运维知识库it looking for exactly when it says td + td? The program is about taking data from a table, so would td + td pick out anything between html table tags <td></td> for example?

//use querySelector to find all second table cells
var cells = document.querySelectorAll("td + td");


It's looking for only <td>s which are preceded by another <td> ( http://www.w3.org/TR/CSS2/selector.html#adjacent-selectors ).

So with this HTML, it would match <td>s 2-4 (inclusive):

<table>
    <tr>
        <td>Cell 1</td>
        <td>Cell 2</td>
        <td>Cell 3</td>
        <td>Cell 4</td>
    </tr>
</table>

An example here: http://jsfiddle.net/tMrbA/


If you have a selector foo + bar, it will search for an element foo and then select all elements matching bar which are siblings coming after foo and have the same parent as foo.

0

精彩评论

暂无评论...
验证码 换一张
取 消