may*_*ank 0 html javascript jquery preventdefault
我需要在点击jquery的锚链接时打开弹出窗口.
这是HTML部分
<a href="/waw/jvcc/customerSearch.htm" title="Clear Search" class="clearField" id="clearText">Clear Search</a>
Run Code Online (Sandbox Code Playgroud)
这是Jquery
$("a.clearField").on("click", function(){loadclearSearchPopup()});
function loadclearSearchPopup(obj){
var delay = '';
$(obj).preventDefault;
//popup open code goes here;
return false;
}
Run Code Online (Sandbox Code Playgroud)
我知道我可以通过用href ="#"替换href来解决这个问题但我很好奇为什么event.preventDefault并返回false不起作用?
任何帮助
$(obj).preventDefault;
Run Code Online (Sandbox Code Playgroud)
应该
e.preventDefault();
Run Code Online (Sandbox Code Playgroud)
它是事件的方法,而不是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
});
Run Code Online (Sandbox Code Playgroud)