我用 Flask 服务器端编写了一个网站,用于监视在我运行的其他构建机器上构建的数据。在监控页面上,我有特定机器的当前状态,每隔几秒钟更新一次。我希望能够将鼠标悬停在某些机器的名称上,然后会弹出一个工具提示,显示它正在构建的当前数据。我目前内置了将数据服务器端存储在数据库中并轮询数据库的功能。但到目前为止,它返回所有数据的状态。我对此表示同意,但我无法区分它位于哪个元素上,因此无法显示正确的数据。在我的 mouseover 函数中,我有一个 ajax 调用,在 success 函数中,我完成了当前 DOM 元素的信息。
$('.target').mouseover(function() {
$.getJSON('/_get_platform', {
statusID: {{ statusID }}
}, function(data) {
var $this=$(this);
var $tip=$($this.attr('data-tooltip'));
}); //end getjson
}); //end mouseover
我想将 $tip 与(例如)id #debugTip 进行比较。因此,如果我知道鼠标位于 id debugTip 的元素上,那么我可以显示与该元素关联的适当数据。
(为了简洁起见,我删除了所有无关的代码,例如工具提示等)
谁能告诉我如何将其与我知道的东西进行比较?我尝试过使用 $tip == '#debugTip' 和 $tip.is('#debugTip') 但要么不起作用,要么我没有正确使用它。有人知道我的错误是什么吗?
提前致谢!!
最佳答案
在执行 ajax 请求之前,您只能访问 this
。因此,您必须在进入 ajax 回调之前存储提示。
$('.target').on( 'mouseover', function() {
// get the tip, while we still have 'this' object (before the ajax)
var $tip = $($(this).data('tooltip'));
$.getJSON('/_get_platform', {
statusID: {{ statusID }}
}, function(data) {
// check if it's a debug tooltip
if ( $tip.is('#debugTip') ) {
console.log("it's the debug tip");
} else {
console.log("normal tooltip");
}
}); //end getjson
}); //end mouseover
希望这有帮助。
关于javascript - $this 选择器与 JQuery 的比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32214377/