我正在使用名为 webuiPopover 的 jQuery 插件。它向链接添加了一个弹出窗口。当用户将鼠标悬停在链接上时,将通过 AJAX 获取弹出窗口的内容。这需要带有适当参数的特定 url
。
所以这是代码:
$(document).ready(function() {
$(".qa-user-link").webuiPopover({
placement:"auto",
trigger:"hover",
type:"async",
cache:false,
url:"./qa-plugin/q2a-user-popover/qa-user-popover-details.php?handle="+$(this).attr("data-id")+"&incdir=%2Fhome%2Fpeatar5%2Fpublic_html%2Fbiophilie%2Fqa-include%2F",
content:function(data) {return data;}
});
});
如您所见,我使用 jQuery 的 attr(...)
函数计算“url”。
不幸的是,这一小段代码总是返回“未定义”。
如果我在 content
参数中使用相同的代码($(this).attr("data-id")
)(以给出 function (data) {return $(this).attr("data-id");}
它工作正常。
出了什么问题?
最佳答案
this
指的是 $(document).ready
回调中的 document
。它在 content
回调内部工作,因为插件是 binding当调用时将元素添加到 content
中。
如果您希望每个弹出窗口都有不同的 url,则必须为每个元素单独绑定(bind)弹出窗口插件:
$(document).ready(function() {
$(".qa-user-link").each( function ( ) {
var $this = $(this);
$this.webuiPopover({
placement:"auto",
trigger:"hover",
type:"async",
cache:false,
url:"./qa-plugin/q2a-user-popover/qa-user-popover-details.php?handle="+$this.attr("data-id")+"&incdir=%2Fhome%2Fpeatar5%2Fpublic_html%2Fbiophilie%2Fqa-include%2F",
content:function(data) {return data;}
});
});
});
关于jQuery 函数参数中的 attr(...) 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28003644/