event.preventDefault()不会阻止无法清除迁移历史记录的普通<a> action

时间:2015-06-22 22:12:21

标签: javascript jquery html jquery-draggable

I am making dragscroller for my div. The draging works, but the div which I drag around has some <a> tags inside. When I grab the div at the same position as the <a> tag (or link) it still redirects me to the linked page.

I am using these codes to prevent them from redirecting after a grab:

//Inside the mouseMoveHandler
    if(delta!=0){
        prevent=1;
    }
//Inside the mouseUpHandler
    if(prevent){
        alert('at this point he should prevent clicking on a link, but he does not');
        prevent=0;
        event.preventDefault();
        return false;
    }

The alert is appearing on the screen, but the event.preventDefault(); and return false; do not seem to work.

I also tried event.stopPropagation() to stop the default link to occur without result.

The snippets codes above can also be found in the complete code bellow:

var dragscroll= {
            mouseDownHandler : function(event) { // mousedown, left click
                // Initial coordinates will be the last when dragging
                event.data.lastCoord = event.clientY; 
                $.event.add( document, "mouseup", dragscroll.mouseUpHandler, event.data );
                $.event.add( document, "mousemove", dragscroll.mouseMoveHandler, event.data );
            },
            mouseMoveHandler : function(event) { // User is dragging
                // How much did the mouse move?
                delta = event.clientY - event.data.lastCoord;
                // Set the scroll position relative to what ever the scroll is now
                event.data.scrollable.scrollTop(event.data.scrollable.scrollTop() - delta);
                // Save where the cursor is
                event.data.lastCoord=event.clientY
                if(delta!=0){
                    prevent=1;
                }
            },
            mouseUpHandler : function(event) { // Stop scrolling
                    $.event.remove( document, "mousemove", dragscroll.mouseMoveHandler);
                    $.event.remove( document, "mouseup", dragscroll.mouseUpHandler);
                if(prevent){
                    alert('at this point he should prevent clicking on a link');
                    prevent=0;
                    event.preventDefault();
                    return false;
                }

            }
    }

1 个答案:

答案 0 :(得分:0)

尝试将另一个处理程序附加到检查变量的所有标记,如果为真则返回。名称变量拖动。在鼠标按下时将拖动设置为false。

问题是事件传播出来所以如果你在容器上设置preventDefault,那么事件不会转到它的父级而不会影响孩子,因为他们是将事件传递给它的人