jquery - 从函数调用中获取 html 属性
我试图根据锚标记的 id 属性调用不同的工具提示。我的 JavaScript 代码如下:
$(function()
{
$('.tippy').qtip(
{
content: {
url: "http://mydomain.com/product/mini/" + $(this).attr("id") // doesn't work
},
hide: { when: 'mouseout', fixed: true },
position: {
corner: {
target: 'bottomRight',
tooltip: 'topLeft'
}
}
});
});
我的 html 代码如下所示:
<div>this is the text and I would like to reference the <a href="product.php" class="tippy" id="123456">superproduct</a> with qtip.</div>
我很困难,你能帮我一下吗?
I'm trying to call a different tooltip according to an anchor tag's id attribute. My JavaScript code is as follows:
$(function()
{
$('.tippy').qtip(
{
content: {
url: "http://mydomain.com/product/mini/" + $(this).attr("id") // doesn't work
},
hide: { when: 'mouseout', fixed: true },
position: {
corner: {
target: 'bottomRight',
tooltip: 'topLeft'
}
}
});
});
my html code looks like this:
<div>this is the text and I would like to reference the <a href="product.php" class="tippy" id="123456">superproduct</a> with qtip.</div>
I'm pretty stuck, could you give me a hand please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
each()
:$(this的原因)
在你的代码中不起作用很简单:它不在函数调用内部(至少不在this
应该引用.tippy 的地方)
元素)。$(this).attr()
在构造传递给qtip()
的对象时执行。这意味着它与$('.tippy')
处于相同的上下文中,因此this
最有可能指的是window
。Use
each()
:The reason why
$(this)
does not work in your code is simple: It is not inside a function call (at least not wherethis
should refer to a.tippy
element).$(this).attr()
is executed when you construct the object that is passed toqtip()
. That means it is in the same context as$('.tippy')
and thereforethis
most probably refers towindow
.