javascript - Mouseup取消所有Mousedown下的事件

标签 javascript jquery html css

我正在尝试使用 Jquery 实现 div 调整代码大小。参见 here .

现在它会随着鼠标的移动而移动。代码在这里:

$('#top-left').mousedown(function(){
    event.stopPropagation();
    $(document).mousemove(function(event){
        var current_width=$(".active").width();
        var current_height=$(".active").height();
        var position = $(".active").position();

        var new_width=current_width+(position.left-event.pageX);

        temp=$("#mouse").html();
        $("#mouse").html(temp+"<br />new_width= "+new_width);

        $(".active").css({top:event.pageY+"px"});
        //$(".active").css({width:new_width+"px"}); 
    });
});
$('#top-left').mouseup(function(){
    event.stopPropagation();
});

我希望当 mouseup 被调用时,在当前 div.active 中注册的事件被删除。如果我不清楚,请告诉我。

检查 JSFiddle Here .

最佳答案

如果您希望 div.active 没有绑定(bind)任何事件,只需执行以下操作: $('div.active').unbind()

供引用:unbind() api reference

此外,由于您正在处理添加到 DOM 的元素,因此使用 .click() 将不起作用,在处理动态元素时,我们引用 on() here .

但是,因为元素是动态的,所以您需要使用 on() 方法的“委托(delegate)”功能:

$( "parentElement" ).on( "click", "clickedElement", function() {
    alert( $( this ).text() );
    console.log(this); // will reference "clickedClicked", not "parentElement"
});

简而言之,委托(delegate)通过监听从子元素冒泡到它的非动态父元素的事件来工作。因此,由于动态子元素的事件会冒泡,父元素将始终收到来自其子元素的点击。

关于javascript - Mouseup取消所有Mousedown下的事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21488814/

相关文章:

javascript - 在 Node.js 中的两个独立运行的进程之间发送消息

javascript - 全局变量未附加到 MeteorJS 包上的 `global` 对象

javascript - 回调函数的 mocha-phantomjs 测试用例

javascript - 搜索数据为空

jQuery 更改单击图像的图像 onclick 和其他图像

javascript - 如何使用 jQuery 仅从内联样式获取 css 属性?

javascript - 我可以使用 JavaScript 变量将 document.write 写入另一个 html 标签吗?

javascript - Bootstrap 表行选择

javascript - 如何更新 getElementsByClassName 返回的多个 span 元素的 text/innerHTML?

java - 将 Java 图形呈现为 HTML