gpt4 book ai didi

函数参数中的 jQuery attr(...) 未定义

转载 作者:太空宇宙 更新时间:2023-11-04 16:22:39 25 4
gpt4 key购买 nike

我正在使用一个名为 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”。不幸的是,那段代码总是返回“undefined”。

如果我在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com