search

Home  >  Q&A  >  body text

javascript - A question about jquery selectors

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?

伊谢尔伦伊谢尔伦2802 days ago971

reply all(5)I'll reply

  • 怪我咯

    怪我咯2017-05-18 10:47:42

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

    reply
    0
  • 怪我咯

    怪我咯2017-05-18 10:47:42

    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 );
    }); 

    reply
    0
  • 巴扎黑

    巴扎黑2017-05-18 10:47:42

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

    reply
    0
  • PHP中文网

    PHP中文网2017-05-18 10:47:42

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

    reply
    0
  • 世界只因有你

    世界只因有你2017-05-18 10:47:42

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

    reply
    0
  • Cancelreply