2013-02-11 45 views
0

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

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

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

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

//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; 
}; 
+0

请发布一些更多的代码。 – Blazemonger 2013-02-11 21:14:13

+1

这可能发生的唯一方法是如果$ tooltip里面有JavaScript触发点击事件。 – 2013-02-11 21:16:58

+0

@KevinB我看遍了各地的任何可能性的新元素包含触发器,但不存在。我从字面上只是将div添加到另一个div。 – tuck 2013-02-11 21:33:08

回答

0

我应该在我的代码中看到这个。我结束了检查$(body).on(click)以确定事件类型event.type,并返回“DOMNodeInserted”。简单搜索,我找到了.on('DOMNodeInserted', function(event) {其中有$('body').trigger('click');

谢谢大家的帮助。

0

没有看到你的代码,当我想Javascript来停止射击,我用event.stopPropagation

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

希望这有助于。

+0

我试图躺在e.stopPropagation()遍布的地方试图阻止它,但它似乎唯一的代码,防止点击是删除我说的一行。 – tuck 2013-02-11 21:22:10