我正在尝试阻止点击事件的触发,我已经完成了我能想到的一切。发生的事情是你单击一个提交按钮,然后进行一些验证,如果失败,我会提供一个工具提示。要显示工具提示,请输入$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;
};
答案 0 :(得分:0)
在没有看到您的代码的情况下,当我希望Javascript停止触发时,我使用event.stopPropagation
:
http://api.jquery.com/event.stopPropagation/
希望这有帮助。
答案 1 :(得分:0)
我应该在我的代码中看到这一点。我最终检查$(body).on(单击)以确定事件类型event.type
并返回“DOMNodeInserted”。简单搜索,我发现.on('DOMNodeInserted', function(event) {
有$('body').trigger('click');
感谢大家的帮助。