{ -6ren">
gpt4 book ai didi

javascript - 无法将文本值分配给事件处理程序中 JQuery 元素的变量

转载 作者:行者123 更新时间:2023-11-30 23:42:42 26 4
gpt4 key购买 nike

这是我的 dom 中项目列表的一些事件处理。
使用事件委托(delegate),点击处理程序位于父容器上

$( "#list" ).on( "click", "a", (event) => {

event.preventDefault();
event.stopImmediatePropagation();

let text = $(this).parent().children().last().text();

// The following HAS content (seen in the dev tools debugger !!!!)
// $(this).parent().children().last().text()

// But the text variable gets No content - WHY ?
// "" is printed....
console.log(text);
});

有什么想法吗?如有任何帮助,我们将不胜感激。

最佳答案

因为你使用了箭头函数,而箭头函数中的this就是浏览器中的window

$( "#list" ).on( "click", "a", function (event)  {

event.preventDefault();
event.stopImmediatePropagation();

let text = $(this).parent().children().last().text();

debugger
console.log(text);
});

关于javascript - 无法将文本值分配给事件处理程序中 JQuery 元素的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60639292/

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