【问题标题】:How do I perform an action on mousedown when a certain element is NOT clicked [duplicate]未单击某个元素时如何对 mousedown 执行操作[重复]
【发布时间】:2011-11-14 14:02:43
【问题描述】:

如果点击不在 rl_menu/它的任何子元素(.comment、.rl_arrow 等)上,我想要执行命令的代码如下

TBR.mouse_down = function(e){
    target = $j(this)
    if (target != $j("#rl_menu") && target != $j("#rl_menu .comment") &&
    target != $j("#rl_menu .change") && target != $j("#rl_menu .rl_arrow_border") &&
    target != $j("#rl_menu .rl_arrow")){
        TBR.Menu.hide(); // this needs to be triggered when the menu is not clicked on.
    }
}

但是当我检查目标时,目标是文档。

回顾一下:

文档中的任何地方,当鼠标按下时,TBR.Menu.hide() 应该被执行。但如果点击在#rl_menu 内,则不应执行TBR.Menu.hide()

我是不是走错路了?

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

    你能写得更简单一点吗?

    $(function(){
    
        $("body").click(function(e){
            var t = $(e.target);
            if( !t.is("#rl_menu") && t.closest("#rl_menu").length == 0)
            {
                alert("CLICKD OUTSIDE THE GREEN")
            }
        });
    });
    

    现场演示:http://jsfiddle.net/JBuJm/

    【讨论】:

      【解决方案2】:

      如果事件冒泡,event.target 将指向事件绑定的元素。要获得被点击的元素,请使用event.originalEvent.target

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-27
        • 2016-09-05
        • 1970-01-01
        • 2016-06-17
        • 2011-07-08
        相关资源
        最近更新 更多