2013-02-11 3 views
0

Я пытаюсь предотвратить событие щелчка при стрельбе, и я сделал почти все, о чем я могу думать. Что происходит, вы нажимаете кнопку отправки, которая затем выполняет некоторую проверку, и если это не удается, я представляю всплывающую подсказку по этой причине. Чтобы показать наконечник инструмента, введите $element.append($tooltip), который затем вызывает $(body).on('click', function() {})append() триггеры on ('click')

Я проверил, что если я удалю добавление, щелчок не будет запущен.

Кто-нибудь знает, почему добавить его, вызвав событие click?

Подробнее Код. Это фактический код ToolTip:

//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

+1

Единственный способ, которым это может случиться, это то, что $ tooltip имеет javascript внутри него, который запускает событие click. –

+0

@KevinB Я посмотрел повсюду на любую возможность нового элемента, содержащего триггер, но не существует. Я буквально просто добавляю div в другой div. – tuck

ответ

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