0

我试图阻止点击事件触发,我已经做了我能想到的所有事情。发生的事情是您单击提交按钮,然后进行一些验证,如果失败,我会给出一个工具提示。为了显示我输入的工具提示$element.append($tooltip),然后触发$(body).on('click', function() {})

我已经验证,如果我删除附加,则不会触发点击。

有谁知道为什么附加它会触发点击事件?

这是更多代码。这是实际的工具提示代码:

//Sets up a tooltip to display feedback
//position: top, bottom or right
NG.Tooltip = function (target, message, timeout, position, doAdjust) {
    var tt = (this == NG ? {} : this); //Prevent people from accidentally missing the "new" keyword and corrupting the global NG object
    var $target = $(target);
    var $targetParent = $target.parent();
    var $offsetParent = $target.offsetParent();
    tt.targetElement = $target.get(0);
    if(!tt.targetElement){
        return;
    }

    //Remove previous tooltip if there is one already attached to target element
    if (tt.targetElement.ngTooltip)
        tt.targetElement.ngTooltip.Remove();

    if (!timeout) timeout = 2000;

    var $div = tt.div = $('<div class="ngTooltip">' + message + '</div>');
    var pos = $target.position();
    var posParent = $targetParent.position();
    if (position == 'bottom') {
        $div.css({ "left": (pos.left) + "px", "top": (pos.top + $target.outerHeight() + 10) + "px" });
    }
    else if (position == 'top') {
        $div.css({ "left": (pos.left) + "px", "top": (pos.top) + "px" });
    }
    else if (position == 'parent-bottom-left') {
        $div.css({ "left": (posParent.left) + "px", "top": (pos.top + $target.outerHeight() + 10) + "px" });
    }
    else {
        $div.css({ "left": (pos.left + $target.outerWidth() + 10) + "px", "top": pos.top + "px" });
    }

    // commenting out this allows the code to go without triggering the click
    //$offsetParent.append($div);

    if (position == 'parent-bottom-left') {
        if ((posParent.left + $div.width()) > $offsetParent.width()) {
            $div.css("width", ($offsetParent.width() - (posParent.left + 15)) + "px");
        }
    }

    //Default to adjust it onscreen.
    //But there are certain cases such as popping up the url of a (link) on hover where adjusting it would not work out well
    if (doAdjust == null || doAdjust == true) {
        NG.AdjustPopupOnScreen($div);
    }

    tt.Remove = function (elem) {
        if (tt.timeoutId)
            window.clearTimeout(tt.timeoutId);
        $div.unbind('click'); //removes click event attached
        $div.remove();
        tt.targetElement.ngTooltip = null;
    };

    $div.click(tt.Remove);
    tt.timeoutId = null;
    if (timeout > 0)
        tt.timeoutId = window.setTimeout(function () { tt.Remove(); }, timeout);

    tt.targetElement.ngTooltip = this;
    return this;
};
4

2 回答 2

0

我应该在我的代码中看到这一点。我最终检查了 $(body).on(click) 以确定事件类型event.type,它返回“DOMNodeInserted”。简单的搜索,我发现.on('DOMNodeInserted', function(event) {$('body').trigger('click');

谢谢大家的帮助。

于 2013-02-11T21:58:49.187 回答
0

在没有看到您的代码的情况下,当我希望 Javascript 停止触发时,我使用event.stopPropagation

http://api.jquery.com/event.stopPropagation/

希望这可以帮助。

于 2013-02-11T21:16:10.493 回答