关于table表格td和tr单击和双击事件的问题

Mr.姚先森 2019-09-09 04:28:07
在tr中有一个单击和双击事件,td中有一个单击事件,连续两次点击td时发现会调用tr中的双击事件,如何进行阻止?不允许调用tr中的双击事件,希望有具体代码例子
...全文
1248 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Hello World, 2019-09-10
  • 打赏
  • 举报
回复
你把td的双击事件冒泡阻止了,tr的双击事件怎样触发?
没有办法避开td去双击tr的,除非仅对部份单元格设置
前端_小菜鸟 2019-09-09
  • 打赏
  • 举报
回复
阻止冒泡行为就可以了,单击事件的时候点击td就不会调用tr上的事件
cn00439805 2019-09-09
  • 打赏
  • 举报
回复

<html>
<body>
    <style>
        td{background:#f60;padding:20px;}
    </style>
    <table>
        <tr>
            <td>1</td>
        </tr>
    </table>
    <script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
    <script>
        var timer1 = timer2 = null
        $("table").on("click","tr",function(e){
            clearTimeout(timer1)
            timer1 = setTimeout(function(){
                console.log("单击了tr")
            },300)
        })
        $("table").on("click","td",function(e){
            clearTimeout(timer2)
            timer2 = setTimeout(function(){
                console.log("单击了td")
            },300)
        })
        $("table").on("dblclick","tr",function(e){
            clearTimeout(timer1)
            clearTimeout(timer2)
            console.log("双击了tr")
        })
        $("table").on("dblclick","td",function(e){
            clearTimeout(timer1)
            clearTimeout(timer2)
            e.stopPropagation()
            console.log("双击了td")
        })
    </script>
</body>
</html>
天际的海浪 2019-09-09
  • 打赏
  • 举报
回复
给td也加个双击事件,在td的双击事件中阻止事件冒泡event.stopPropagation()
天际的海浪 2019-09-09
  • 打赏
  • 举报
回复
给td也加个双击事件,在事件阻止冒泡event.stopPropagation()

87,907

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧