嘿,我有一个在 anchor 的 onclick 事件上触发的 jquery 函数,函数如下:
function dropDown(subid, e) {
e.preventDefault();
var sub = '#' + subid;
// hide all submenus first
$('.subnav').css({'display' : 'none'});
//Show the Current Subnav
$(sub).css({'display' : 'block'});
}
这就是我尝试触发它的方式:
<a href="#!" onclick="dropDown('sn_cities')" >Cities</a>
但是我收到这个错误:e is undefined
我想取消 anchor 链接的默认 onclick 事件,任何帮助将不胜感激。
最佳答案
您没有将事件对象(或任何东西)传递给函数的 e
参数。试试这个:
onclick="dropDown('sn_cities',event);"
不过,我倾向于完全放弃内联 JS:
<a href="#!" data-dropDown="sn_cities">Cities</a>
$(document).ready(function() {
$("a[data-dropDown]").click(function(e) {
e.preventDefault();
// hide all submenus first
$('.subnav').hide();
//Show the Current Subnav
$('#' + $(this).attr("data-dropDown")).show();
});
});
关于javascript - jquery : event : event. preventDefault(); , e 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10826354/