gpt4 book ai didi

javascript - 为什么 myvar = $(this) 之后 myvar 未定义

转载 作者:行者123 更新时间:2023-12-02 18:21:18 26 4
gpt4 key购买 nike

所以我正在制作一个关于沙漠幸存者的小游戏。幸存者在返回淘金鬼城的途中必须从遍布沙漠的水井里喝水。有些井可以饮用,但有些井却有毒。我在表中具有“well”类的 TD 元素上显示工具提示。在工具提示的初始化对象内,我需要获取对当前 TD 元素的引用,以便可以将其传递给设置工具提示的“content”属性的函数。在该函数中,我必须测试当前的 TD 是否也具有“中毒”类。

function initWellsTooltip() {
$("#water-table tbody td.well").tooltip({

content: function () {
var well$ = $( this ); //
// at this point stepping through the code in the debugger,
// well$ is undefined and I don't understand why,
// because $(this).hasClass("poisoned") succeeds.
// VS2010 debugger shows as follows:
// ?$(this).hasClass("poisoned")
// true
// ?well$
// 'well$' is undefined
if (well$.hasClass("poisoned")) {
return "poisoned!";
} else {
return "potable";
}

},
items: "td.well",
position: { my: "left+15 center", at: "left top" }

});
}

最佳答案

由于 td.well 数量多于一个,因此您必须迭代它们以设置正确的 well$

function initWellsTooltip() {
$("#water-table tbody td.well").each(function() {
var well$ = $(this);

well$.tooltip({
content: function () {
return well$.hasClass("poisoned") ? "poisoned!" : "potable";
},
items: "td.well",
position: {
my: "left+15 center",
at: "left top"
}
});
});
}

关于javascript - 为什么 myvar = $(this) 之后 myvar 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18801719/

26 4 0
文章推荐: Javascript 将正则表达式与组匹配
文章推荐: javascript - 全局函数表达式如何访问JavaScript中包含函数内部的变量
文章推荐: javascript - 更改非事件页面标题
文章推荐: javascript - 错误: Object # has no method 'easeInOutCubic' - jquery-ui