javascript - A question about jquery selectors
伊谢尔伦
伊谢尔伦 2017-05-18 10:45:42
0
5
957

Now there is a table, each tr has a number as class. Assume that a tr is selected. How to get all tr ​​whose class is less than or equal to the selected tr?

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(5)
刘奇

You can only select in a loop. What is the size of the number?

刘奇

You should not use tr numbers as classes, because there are many classes bound to tr. You can completely bind numbers to data-num. The logic is probably as follows. Maybe some selectors are not written so accurately

html table:

<table id='example_table'>
    <thead>
        <th>1<th>
        <th>2<th>
        <th>3<th>
    </thead>
    <tbody>
        <tr data-num="1">
            <td>1<td>
            <td>2<td>
        </tr>
        <tr data-num="2">
            <td>1<td>
            <td>2<td>
        </tr>
        <tr data-num="3">
            <td>1<td>
            <td>2<td>
        </tr>
    </tbody>
</table>

js:

$('#example_table tr').on('click', function(e) {
    var select_tr_num = $this.data('num');
    var request_trs = [];
    $.each($('#example_table tr'), function(i, obj) {
        if (!obj.data('num') > select_tr_num) {
            select_tr_num.push(obj);
        }
    });
    console.log(request_trs );
}); 
巴扎黑

What the first floor said is right. Generally, no one names a class with a number. They usually add a custom attribute

Ty80

Traverse the values ​​of all classes and then compare and store them. (ps: But using numbers as classes is not very standard.)

世界只因有你

Sort by class, and then get all the tr before the target tr

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template