假设您使用的是 jQuery,请将悬停事件绑定到用户名链接。因此:
$('.username').hover(function (e) {
console.log("i'm hovering!! on id: "+$(this).attr('data-user-id')); //See the next step for where this came from
}
接下来,将用户的 id 添加到用户名元素中,可能在数据属性中:
<span class="username" data-user-id="1234567890">Username</span>
接下来,记录哪些用户已经加载,可能是通过 id。当您获取新内容时,将其添加到对象中。我喜欢把这样的物体放在窗户上。
window.loadedUserInfo = {};
悬停时检查此对象中是否存在 userId 键。如果是,请使用它。如果没有,请使用 ajax 调用来获取它:
$.ajax({
url : "path/to/userinfo"+userid, //I'm assuming you're using restful endpoints
type : "GET",
success : function (res) {
window.loadedUserInfo[userid] = res;
//Format your popover with the info
},
error: function (jqxhr) {
//something went wrong
}
})
至于弹出框本身,您可能可以使用引导弹出框。
把它们放在一起:
$(".username").hover(function (e) {
console.log("i'm hovering!! on id: "+$(this).attr("data-user-id")); //See the next step for where this came from
if (typeof window.loadUserInfo[$(this).attr("data-user-id")] == 'undefined') {
$.ajax({
url : "path/to/userinfo"+userid, //I'm assuming you're using restful endpoints
type : "GET",
success : function (res) {
window.loadedUserInfo[userid] = res;
//Format your popover with the info
},
error: function (jqxhr) {
//something went wrong
}
})
} else {
//populate popover with info in window.loadUserInfo[$(this).attr('data-user-id')]
}
}