我在jquery中有一个工作的a.click()函数... 但是如果我点击一个锚点,我会打开一个新窗口...但是如何阻止浏览器打开一个新窗口呢?
示例:
$('a').each(function() {
$(this).click(function(e) {
if($(this).attr('target') == '_popup') {
//here is code to open window (already exists)
//try to stop propagation, but doesn't work...
e.stopPropagation();
}
hideNSL();
});
});
那么,我该如何停止事件?
答案 0 :(得分:12)
尝试
e.preventDefault()
但是return false
也可以在另一个答案中执行此操作。 preventDefault
可用于更多的senarios,是一种更通用的“停止默认事件操作”,请参阅:http://api.jquery.com/event.preventDefault/
答案 1 :(得分:4)
将此添加到点击事件的结尾:
return false;
例如:
$('a').each(function() {
$(this).click(function(e) {
var toReturn = true;
if($(this).attr('target') == '_popup') {
//here is code to open window (already exists)
toReturn = false;
}
hideNSL();
return toReturn;
});
});
答案 2 :(得分:3)
你可以尝试
e.preventDefault();
答案 3 :(得分:3)
e.preventDefault();
答案 4 :(得分:-1)
编辑,我认可.preventDefault不是jQuery函数。