我读了下面的内容,但还是想讨论一下...Meaning of evt = (evt) ? evt : window.event
//是否需要传递evt,因为它不在函数中使用?-尝试让我的注意力集中在这个jQuery内容上。
a)
$('#' + _ids.btnPrintServiceCode).on('click', function (evt) {
alert('Hello World')
});或
b)
$('#' + _ids.btnPrintServiceCode).on('click', function () {
alert('Hello World')
});发布于 2013-05-01 02:01:35
如果不使用evt,则不需要传递它...这是可选的。然而,拥有它不会造成问题……
基本上,如果您需要监听事件,则传递evt ...假设你想阻止某些元素的默认行为,所以你需要evt
HTML
<a href="#"></a>jquery
$('a').on('click', function (evt) {
evt.preventDefault(); // this will prevent the deafult behaviour of that element
alert('Hello World');
});
$('input').keyup(function(e){ //here we need to pass e as we are using e to get ther keyCode that was pressed
if(e.keyCode == 8 )
{
alert('test');
}
});发布于 2013-05-01 02:11:54
这样想吧。假设我有这个函数:
function foo (bar) {
alert("worky");
}
foo("foobar"); // worky因为在函数中我从来没有实际使用过bar,所以我可以通过从参数列表中删除bar来简化函数。
function foo () {
alert("worky");
}
foo("foobar"); // worky两者都会有相同的结果。事件绑定也是如此。因为你从来没有使用过evt,所以你没有理由把它传递给这个函数,但是如果你这样做了,它也不会有什么坏处。
如果以前的开发人员总是将evt传递给事件处理程序回调,您应该继续这样做,以保持代码的一致性,但这完全取决于您。
https://stackoverflow.com/questions/16306154
复制相似问题