复杂的jquery选择器:挑战
我已经在 jquery 选择器 中搜索了一段时间,但找不到任何解决方案我的问题。
我有一个由 foreach 归档的 html 表。每行都有几个弹出工具提示的链接。我的问题:找不到正确的选择器。
<table>
<?php foreach($article) :?>
<tr>
<td>
<div class="none" style="display:none;">
<div class="tooltip_1">
"The content of my tooltip_1"
</div>
<div class="tooltip_2">
"The content of my tooltip_2"
</div>
</div>
<div class="cell">
<a href="#" class="link_to_tooltip_1">a link</a>
<a href="#" class="link_to_tooltip_2">a link</a>
</div>
</td>
<tr>
<?php endforeach; ?>
</table>
为了显示我的工具提示,我使用 qTip,它的工作原理如下:
$('a[class="link_to_tooltip_1"]').qtip({
content: $('jquery selector'),
(... other options)
});
所以基本上,我会需要类似的东西
content: $('self.parentNode.parentNode > div[class="none"] > div[class="tooltip_1"]'),
:
- 从链接“link_to_tooltip_1”开始,
- 返回父div“cell”,
- 返回父td,
- 然后转到子div“none”
- ,最后选择子div“tooltip_1”,
非常感谢。
I've searched among jquery selectors for some time now, but can't find any solution to my problem.
I've got an html table filed by a foreach. On each line, several links that pop up tooltips. My problem : can't find the right selector.
<table>
<?php foreach($article) :?>
<tr>
<td>
<div class="none" style="display:none;">
<div class="tooltip_1">
"The content of my tooltip_1"
</div>
<div class="tooltip_2">
"The content of my tooltip_2"
</div>
</div>
<div class="cell">
<a href="#" class="link_to_tooltip_1">a link</a>
<a href="#" class="link_to_tooltip_2">a link</a>
</div>
</td>
<tr>
<?php endforeach; ?>
</table>
To show my tooltip, I use qTip, and it works like this :
$('a[class="link_to_tooltip_1"]').qtip({
content: $('jquery selector'),
(... other options)
});
So basicaly, I would need something like
content: $('self.parentNode.parentNode > div[class="none"] > div[class="tooltip_1"]'),
in other words :
- start from link "link_to_tooltip_1"
- go back to parent div "cell"
- go back to parent td
- then go to child div "none"
- and finally select child div "tooltip_1"
Thanks a lot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
jsFiddle 链接
jsFiddle Link
可能是您正在寻找的?
is probably what you're seeking for ?
为什么不这样放置你的工具提示呢? :
和
编辑
我不知道你不能使用 $(this)。所以在这种情况下,你可以这样做:
why not place your tooltip like this ? :
and
Edit
I didn't know you can't use $(this). So in this context, you can do :
这是您正在寻找的选择器:
说明:
您不能向后(匹配一个元素,然后匹配其父元素)。但是,您可以选择一个元素并验证它是否包含与其他选择器匹配的元素:
这会选择
.link_to_tooltip_1
的父级。因此,这正是您所描述的
.link_to_tooltip_1.parentNode.parentNode
。然后,您只需在选定的
中选择
.none .tooltip_1
:因此您的示例代码变为:
正如您所要求的,这只需完成即可一个 jquery 选择器:-)
Here is the selector you are looking for:
Explanations:
You can't go backwards (match an element, and then its parent). However you can select an element and verify that it contains elements that match an other selector:
This selects the parent
<td>
of.link_to_tooltip_1
. So this does exactly the.link_to_tooltip_1.parentNode.parentNode
you described.Then you just have to select
.none .tooltip_1
in the selected<td>
:So your example code becomes:
And as you were asking for, this is done with just a jquery selector :-)
我会尝试这样的事情:
使用工具提示和 rel 属性向元素添加一个类,其中包含保存数据的元素的目标类,
然后在 JS 中,
这是我可以用来欺骗缺乏对基于通用数据引用的支持的唯一想法DOM 结构。虽然不能保证它会起作用,因为我不知道该插件,也不知道将函数作为参数传递是否不会与它的实现方式发生冲突 - 您可能必须更改插件以允许它接受函数内容参数。
再见,祝你好运,
汤姆
I'd try something like this:
add a class to the elements with tooltips and rel attribute with the target class of element holding data
then in JS
it's the only idea I can come with to cheat the lack of support for generic data referencing based on DOM structure. Though can't promise it will work as I don't know the plugin and don't know if passing function as an argument won't collide with how it's implemented - you might have to change the plugin to allow it to accept function as content parameter.
good bye and good luck,
Tom