我需要使用 jquery 单击 anchor 链接打开弹出窗口。
这是 HTML 部分
<a href="/waw/jvcc/customerSearch.htm" title="Clear Search" class="clearField" id="clearText">Clear Search</a>
这里是 Jquery
$("a.clearField").on("click", function(){loadclearSearchPopup()});
function loadclearSearchPopup(obj){
var delay = '';
$(obj).preventDefault;
//popup open code goes here;
return false;
}
我知道我可以通过将 href 替换为 href="#"来解决此问题 但我很好奇为什么 event.preventDefault 和 return false 不起作用?
任何帮助
最佳答案
$(obj).preventDefault;
应该是
e.preventDefault();
它是事件的方法,而不是 jQuery 对象的属性。另外,return false
不起作用的原因是您没有将返回值传递回处理程序
$("a.clearField").on("click", function (e){
var delay = '';
// Prevents the link from being followed
e.preventDefault();
// Prevents following links and propagation (bubbling the event)
// Note that this is a jQuery feature only. In standard DOM event handlers,
// return false is the same as e.preventDefault()
return false;
// But you don't need both
});
关于javascript - anchor 链接不适用于 jquery event.preventDefault;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21495873/