开发者社区> 问答> 正文

javascript 给元素绑定事件

问题描述:给div元素绑定了mousemove事件,div 内部还有其他子元素比图button、input等。问题出现了,div 内部的所有元素都被绑定了同样的事件

部分代码:

$(".gridGlobal").each(function(){
        $(this).mousedown(function (event){
        
        theDiv = $(this);
                  
            range.x = event.pageX - theDiv.offset().left;
            range.y = event.pageY - theDiv.offset().top;
            
            theDivId = theDiv.index();
            theDivHeight = theDiv.height();
            theDivHalf = theDivHeight/2;
            move = true;
            theDiv.attr("class","maindash");
            $("<div class='dash'></div>").insertBefore(theDiv);
        });
    });

希望解决:只给div元素绑定事件,div的内部子元素都不受影响

展开
收起
a123456678 2016-07-15 16:15:37 1985 0
1 条回答
写回答
取消 提交回答
  • $(document).mousemove(function(event) {});

    用document 绑定事件造成的

    2019-07-17 19:57:12
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
JavaScript面向对象的程序设计 立即下载
Delivering Javascript to World 立即下载
编程语言如何演化-以JS的private为例 立即下载