gpt4 book ai didi

jquery - 函数(e)和函数()之间的区别

转载 作者:行者123 更新时间:2023-12-01 00:44:24 26 4
gpt4 key购买 nike

以下代码之间的显着性/差异是什么? (e) 最后

jQuery(document).on( 'click', '.something', function(e) {

vs.

jQuery(document).on( 'click', '.something', function() {

谢谢!

最佳答案

从技术上讲,这两种表达方式没有区别。 'e' 指的是事件变量,它是可选的,其工作方式更像是 jquery 中的 this 表达式。您可以使用该 e 变量来找出某些信息,例如调用事件的目标或任何其他属性。

jQuery(document).on( 'click', '.something', function() {
alert(this.id); // gives you the id of the element using this
});

jQuery(document).on( 'click', '.something', function(e) {
alert(e.target.id); // gives you the id of the element using event
});

在我看来,使用事件 e 的最大优点是,当通过 调用事件处理程序时,与 this 相比,它可以为您提供更正确的信息。 >文档

$(document).on('click',function()
{
alert($(this).attr("id")); // id as undefined
})

$(document).on('click',function(e)
{
alert(e.target.id); // gets the correct id
})

示例:http://jsfiddle.net/twjwuq92/

关于jquery - 函数(e)和函数()之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34142022/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com