JQuery, select first row of table

前端 未结 6 1009
清歌不尽
清歌不尽 2020-12-30 00:26

I\'ve used JQuery to add a \"image\" button to a few rows in a table. I used the following code:

$(\"#tblResults tr:nth-child(1) td.ResultsHeader span.gridRC         


        
6条回答
  •  有刺的猬
    2020-12-30 00:45

    Ok so if an image in a table is clicked you want the data of the first row of the table this image is in.

    //image click stuff here {
    $(this). // our image
    closest('table'). // Go upwards through our parents untill we hit the table
    children('tr:first'); // Select the first row we find
    
    var $row = $(this).closest('table').children('tr:first');
    

    parent() will only get the direct parent, closest should do what we want here. From jQuery docs: Get the first ancestor element that matches the selector, beginning at the current element and progressing up through the DOM tree.

提交回复
热议问题