【发布时间】:2016-08-16 09:13:39
【问题描述】:
据我所知e 代表 Javascript 方法中的“事件”,但我不明白如果我想在调用它们时在 Javascript 方法中使用下面提到的某些属性怎么办。
例如我可以在这个方法中使用e.preventDefault()或e.ctrlKey:
$(".menuItem").click(function (e) {
e.preventDefault();
//Open url in new tab with ctrl key press
if (e.ctrlKey) {
window.open(url, '_blank');
event.stopPropagation();
return false;
}
//... code omitted for brevity
});
但我不能在从表单元素调用的方法中使用它们,即.menuItem。有没有可能在下面那个方法中使用它?
function TestFunction(e, controller, action) {
e.preventDefault();
//Open url in new tab with ctrl key press
if (e.ctrlKey) {
window.open(url, '_blank');
event.stopPropagation();
return false;
}
//... code omitted for brevity
});
};
更新:我调用TestFunction()如下图:
<a onclick="TestFunction('Account', '_Register')" id="test" class="nav-link ">
当我在那个方法中使用 e 时 “Uncaught ReferenceError: e is not defined” 遇到错误。有什么想法吗?
【问题讨论】:
-
如您所见,您的
TestFunction中没有e参数,您需要添加它 -
是的,但是在使用它时,我遇到了问题中提到的错误。我也在问题中添加了 e 参数。有什么想法吗?
-
如何将
TestFunction与点击事件绑定?你的代码中有$(".menuItem").click(TestFunction);吗? -
试试
<a onclick="TestFunction.bind(null,'Account', '_Register')"和TestFunction像这样:function TestFunction(controller, action, e) { -
@T.J.Crowder "TypeError: e is null" 遇到错误。
标签: javascript jquery javascript-objects dom-events