Javascript 为什么在myvar=$之后未定义myvar(此)
所以我在做一个关于沙漠中幸存者的小游戏。幸存者在返回淘金鬼城的路上,不得不从遍布沙漠的水井里喝水。有些井很好喝,但有些井有毒。我正在显示一个关于表中那些TD元素的工具提示,这些元素的类为“well”。在工具提示的初始化对象中,我需要获取对当前TD元素的引用,以便将其传递给设置工具提示的“content”属性的函数。在该函数中,我必须测试当前TD是否也有“中毒”类Javascript 为什么在myvar=$之后未定义myvar(此),javascript,jquery,syntax,Javascript,Jquery,Syntax,所以我在做一个关于沙漠中幸存者的小游戏。幸存者在返回淘金鬼城的路上,不得不从遍布沙漠的水井里喝水。有些井很好喝,但有些井有毒。我正在显示一个关于表中那些TD元素的工具提示,这些元素的类为“well”。在工具提示的初始化对象中,我需要获取对当前TD元素的引用,以便将其传递给设置工具提示的“content”属性的函数。在该函数中,我必须测试当前TD是否也有“中毒”类 $(这个)在那个位置不要参考$(“#地下水位tbody td.well”)。因此,您需要将其更改为$(“#地下水位tbody td.w
$(这个)
在那个位置不要参考$(“#地下水位tbody td.well”)
。因此,您需要将其更改为$(“#地下水位tbody td.well”)
,如下所示
function initWellsTooltip() {
var that = $("#water-table tbody td.well");
that.tooltip({
content: function () {
var well$ = that; //
// 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.wells不止一个,因此您必须迭代它们以设置正确的
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"
}
});
});
}
我不明白<代码>$(“#地下水位tbody td.well”)返回多个井,所有井都是饮用井和中毒井。可能是VS2010?我得到了一个未定义的错误。我要试试Firebug。
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"
}
});
});
}