为什么在myvar = $(this)之后没有定义myvar
why is myvar undefined after myvar = $(this)
所以我正在做一个关于沙漠中幸存者的小游戏。幸存者在返回淘金热鬼城的路上必须从散布在整个沙漠中的水井中喝水。有些井很好喝,但有些则有毒。我在表格中具有"井"类的那些 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"
}
});
});
}
$(this)
instace不是指$("#water-table tbody td.well")
.所以你需要把它改成$("#water-table 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" }
});
}
希望这对你有帮助。
相关文章:
- electronic BrowserWindow的最小高度和宽度在hide()show()方法之后不起作用
- 在chrome.tabs.onCreated之后加载HTML页面
- 在Jquery detachment()和appendTo()之后定位元素
- AngularJS:ng之后,重复$scope值未按预期更新
- 为什么元素的宽度在页面加载之后和那一刻之后不同
- 没有在Angular应用程序中定义firebase(在firebase迁移之后)
- Javascript复选框函数:;缺少:在属性id之后"
- 在刚刚调用的append函数之后,jquery.height()不会返回实际值
- 正在链接添加了以下内容::在之后
- JQuery hide()在show()之后不起作用,反之亦然
- 未执行Ajax循环的Javascript之后的代码
- 如何在`window.open`之后执行回调
- 如何在一个元素动画之后延迟
- 在XMLHttpRequest之后重新初始化jQuery
- <text区域>在我的html中包含event.keycode==13之后,wrap就不起作用了
- 在jAlert之后设置焦点
- 在Javascript中的ajax响应之后未调用Dropdown事件
- web浏览器中的离线应用程序存储数据并在之后上传
- 错误“;未处理'错误'事件“;在运行yo发电机之后
- 为什么在myvar = $(this)之后没有定义myvar