javascript - jQuery获取table中点击位置所在行的td?
PHP中文网
PHP中文网 2017-04-11 11:12:41
[JavaScript讨论组]
  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);
               });
    

`

表格如下:

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

PHP中文网
PHP中文网

认证0级讲师

全部回复(5)
高洛峰
$(".a_see").on('click', function() {
    var arrayContent = [];
    $(this).closest('tr').find('td').each(function() {
        arrayContent.push($(this).not(".operate").text());
    })
    console.log(arrayContent);
});
PHP中文网

根据事件对象的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()); 
    });
});
阿神

直接上代码

<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>
PHP中文网

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

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号