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

标签 jquery html jquery-plugins jquery-attributes

我正在使用名为 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/

相关文章:

jquery - 是否有任何 jQuery 教程演示了接受选择器语法的创作插件?

javascript - 安装和使用 jquery 插件

jquery 单元格选择开始为更多单元格制作 rowspan

jquery - 如何使轮播控制按钮居中?

java - 使用java邮件将图像嵌入到html电子邮件中

javascript - 意外标记 < 在 HTML 的第一行

带有轮播的 jQuery Cycle2 插件无法工作;控制台中没有错误

javascript - 如何在幻灯片中使用框阴影柔化/羽化图像边缘?

javascript - 可拖动的 iFrame 注册点击而不是拖动

html - 如何在悬停时更改内容