Jquery every() 似乎不适用于我的情况
我有一个用户/网站列表,每个用户/网站都有一个横幅。我想通过工具提示显示该横幅。
$(document).ready(function() {
$(".test").each(function(i) {
var banner_url = $("input[name='banner_url']").val();
var index = i + 1;
$(this).tipTip({maxWidth: "auto", edgeOffset: 10, content: banner_url + index});
});
});
但它总是在每个工具提示上显示相同的横幅
(index
var 只是用来测试 each
,它可以工作,但 banner_url
不行)
我也尝试过输入横幅网址var 在 .test every()
之前,但结果相同。有谁知道怎么回事吗?我确信我错过了什么。
<div class="banner">
<input type="hidden" name="banner_url" value="{$banner_url}" />
<a href="{$url}" onclick="out('{$username}');" id="{$username}" class="test" title="">{$title}</a>
</div>
还尝试将这 2 个放入 div 容器中,但这似乎没有什么区别
我可以将横幅代码放入标题属性中,但我想避免必须将 img
硬编码到标题中标签。
感谢您的任何提示,帮助。 塞布
I have a list of users/websites and each of them have a banner. I want to show that banner via a tooltip.
$(document).ready(function() {
$(".test").each(function(i) {
var banner_url = $("input[name='banner_url']").val();
var index = i + 1;
$(this).tipTip({maxWidth: "auto", edgeOffset: 10, content: banner_url + index});
});
});
But it always show the same banner on each tooltip
(index
var is just there to test the each
, and it works, but banner_url
not )
I've also tried to put the banner url var before .test each()
, but with the same results. Anyone know whats up? I am sure I miss something.
<div class="banner">
<input type="hidden" name="banner_url" value="{$banner_url}" />
<a href="{$url}" onclick="out('{$username}');" id="{$username}" class="test" title="">{$title}</a>
</div>
Tried also to put those 2 into a div container, but it doesnt seem to make a difference
I can put the banner code into the title attribute, but I want to avoid having to hard code an img
into title tag.
Thanks for any tips, help.
Seb
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果没有看到整个 HTML,我猜您希望每个容器 .test 中都有banner_url,因此:
否则您总是从文档根目录开始搜索具有该名称的第一个输入,并且当然总是会得到相同的值。
Without seeing the whole HTML, I'm guessing you want the banner_url inside each container .test, so:
Otherwise you're always searching for the first input with that name, starting from the document root, and of course will always get the same value.
我怀疑问题出在您的使用上:
使用该选择器调用 jQuery 的范围仅限于文档,而不是您使用 every() 选取的元素。
在您的 html 中,带有 url 的标签在 DOM 中与您的元素处于同一级别,因此您需要使用适当的 jQuery 搜索功能来定位它。这应该有效:
I suspect the problem is with your use of:
Calling jQuery with that selector is scoped to the document, not the element which you're picking up with each().
In your html, the tag with the url is at the same level in the DOM as your element, so you need to use the appropriate jQuery search function to locate it. This should work: