gpt4 book ai didi

jquery - $(this) 怎么可能不是在 jquery 点击处理程序中触发事件的元素?

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

我有一个简单的点击处理程序

<h1 id="test">
$('#test').click( ev => {
var $test = $(this);
console.log($test.text());
})

但它不起作用,$(this)不是发射器元素。如果我检查this,我会看到一个“窗口”(?) (??) (??!)

另外,如果我将代码更改为

var $test = $(ev.toElement);

它工作得很好。

怎么会这样呢?我的代码中的什么阻止了 jquery 正确传递 this 中的元素?

最佳答案

箭头函数不绑定(bind) thisargumentsMDN .

An arrow function expression has a shorter syntax compared to function expressions and lexically binds the this value (does not bind its own this, arguments, super, or new.target).

关于jquery - $(this) 怎么可能不是在 jquery 点击处理程序中触发事件的元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37655808/

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