简体   繁体   English

在“ mousedown”事件期间添加新元素会阻止触发“ click”事件?

[英]Adding New Element during `mousedown` event prevent triggering `click` event?

I am working with jquery events and I am adding a new element at the time of mousedown at the position of the mouse pointer and after adding the element the binded click event is not triggered. 我正在处理jquery事件,并且在将mousedown时在鼠标指针的位置添加了一个新元素,并且在添加该元素之后,不会触发绑定的click事件。

Any suggestion is appreciable. 任何建议都是可取的。

Thanks in advance. 提前致谢。

Madhu 马杜

Code: 码:

<div style="border:1px solid">Click</div>
<span></span>
<div class="vis" style="display:none">Hello</div>
<script>
    var visualEle = $('div.vis');
    visualEle.css({border:"1px solid"});
    $('a').on("click", function (e) { e.preventDefault();});
    $('div').on("mousedown", mDown);
   $('div').on("mouseup",mUp);
    function mDown(e) {
        e.preventDefault();
        visualEle.css({ left: 100, top: 0, display: "block" });
        }

    function mUp(e) {

        e.preventDefault();
        $('span').append('mouse up triggerd<br/>');
        return false;
    }
    $('p').on("click",dClick);
    function dClick(e) {                
            $('span').append('double click triggerd<br/>');

    }
</script>

In the above code the click event is not triggered after the mousedown is completed. 在上面的代码中,完成mousedown后不会触发click事件。

according to this document 根据这份文件

The click event is sent to an element when the mouse pointer is over the element, and the mouse button is pressed and released 当鼠标指针悬停在元素上,并且按下并释放鼠标按钮时, click事件将发送到该元素

in your case mouse point is over when mousedown happen but after that visualDiv moves under and mouse is released on this element. 在您的情况下,当发生mousedown时鼠标指向结束,但在此之后, visualDiv移至下方,并且在此元素上释放了鼠标。

possible solution I can think of thanks to @ArunPJohny is use $(this).trigger("click"); @ArunPJohny我可以想到的可能解决方案是使用$(this).trigger("click");

$('p').on("mousedown", function (e) {
    _x = e.pageX;
    _y = e.pageY;
    visualDiv.css({
        left: _x,
        top: _y
    });
    $(this).trigger("click");
});
$('p').on("click", function (e) {
    console.log(e.target)
});

Demo: http://jsfiddle.net/c5CRd/ 演示: http//jsfiddle.net/c5CRd/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM