我正在使用 jQuery 2.1.4。
我正在使用最新版本的tipsy。
我知道 jQuery 已.live
被弃用并使用.on
. 我对醉酒进行了更改,如下所示:
和,
if (options.trigger != 'manual') {
var eventIn = options.trigger == 'hover' ? 'mouseenter' : 'focus',
eventOut = options.trigger == 'hover' ? 'mouseleave' : 'blur';
if(options.live){
$(this.context).on(eventIn, this.selector, enter).on(eventOut, this.selector, leave);
} else {
this.on(eventIn, enter).on(eventOut, leave);
}
}
这是我页面中的 Javascript,
$('svg text').tipsy({
gravity: 's',
html: true,
live: true,
title: function() {
$.ajax({
dataType: 'json',
url: "http://myserver/apihistory/emp.php?empid=123456",
success: function(result){
alert(result.lastname);
return result.lastname;
},
error: function(result){
return 'no good';
}
});
}
});
当我将鼠标悬停在元素上时,我从醉意中得到“未定义”。我得到了正确的值alert
。为什么醉醺醺的看不到价值?我的 php 文件正在发送 json 的标头,
header('Content-Type: application/json');
echo json_encode($results);
我有 json 作为我的数据类型,所以我得到一个对象,而不是文本。当我返回 result.lastname 时,我正在发送我的姓氏文本(或者我是这么认为的)。
我知道醉醺醺的工作,因为如果我把 Ajax 拿出来,只用“嗨”。我的工具提示有效,而且我在其他地方使用了它,只是没有与 json 一起使用。
我检查了浏览器开发工具 (Chrome) 中的标题和 Javascript 对象,并手动浏览到该页面。这是我能看到的最好的正确数据和格式(或者我无法提醒结果.姓氏)。
编辑:
我阅读了链接的 SO 问题,我相信我现在明白了,异步部分是我的“问题”,所以我在 .ajax 调用中使用了这些知识:
title: function(){
....
success: function(result){
getLinkage(result.lastname);
},
}
//...outside of tipsy
function getLinkage(response){
return response;
}
但我继续不确定。我认为我的回调不正确。我究竟做错了什么?
编辑:
我改变了这个。这不是回调吗?我意识到发生了什么,但我不明白解决方案。我以为我在这里有一个回调。
醉内:
title: function(){
return getLinkage(); //isn't this the callback?
}
//...outside of tipsy
function getLinkage(){
$.ajax({
dataType: 'json',
url: "http://myserver/apihistory/emp.php?empid=123456",
success: function(result){
alert(result.lastname);
return result.lastname;
},
error: function(result){
return 'no good';
}
});
}