首页 > jQuery获取table中点击位置所在行的td?

jQuery获取table中点击位置所在行的td?

  1. 点击查看后,获取所在行的内容,不包含操作列的内容

  2. `

    $(".a_see").on('click',function(){
                   var arrayContent = [];
                   if($("table tr").length>1){
                       $("table tr").find("td").each(function(){
                        arrayContent.push($(this).not(".operate").text());    
                       //arrayContent = arrayContent.slice(0,6);                
                       })
                   }                
                   console.log(arrayContent);
               });
    

`

表格如下:

这是我打印的结果,我点击查看,所有的表格内容都获取了,但我只需要当前行的数据


直接上代码

<table>
    <tr>
        <td>1</td>
        <td>2</td>
        <td>3</td>
    </tr>
    <tr>
        <td>4</td>
        <td>5</td>
        <td>6</td>
    </tr>
</table>
<script src="//cdn.bootcss.com/jquery/3.0.0/jquery.min.js"></script>
<script type="text/javascript">

    $("table").on("click","tr",function(e){
        var arr = []
        $(this).children().map(function(el){
            arr.push($(this)[0].innerText)
        })
        console.log(arr)
    })
</script>

$(".a_see").on('click', function() {
    var arrayContent = [];
    $(this).closest('tr').find('td').each(function() {
        arrayContent.push($(this).not(".operate").text());
    })
    console.log(arrayContent);
});

点击查看按钮的时候,可以获取到这个按钮所在的td吧?然后不就能获取到这个td的兄弟td了吗?不就能获取到数据了吗?


根据事件对象的e.target.parentNode可获取当前点击列的父节点也就是tr


$(".a_see").on('click','tr',function(){
    var arrayContent = [];
    var $tds=$(this).find("td:not(.operate)");
    $tds.each(function(){
        arrayContent.push($(this).text()); 
    });
});
【热门文章】
【热门文章】