Jquery 遍历 DOM 来查找连续值
感谢您的浏览,所有有用的答案都已投票。 这是我的标记。我试图找到 2 个连续的 div cmk1 和 cmk2
,其内容为 RIGHT
和 HERE
按连续顺序。
div id 1
不应匹配,因为有一个右侧
,但此处
不匹配。div id 3
不应匹配,因为有here
但没有right
。- 我试图找到类似
div id 2
的内容,其中right
后跟here
。另外,文本必须准确:
即使包含单词more more than rightright
也不应该匹配
最有效的方法是什么这?
更新:我只是有一个想法,我可以找到each
class=cmk1
。如果它匹配 RIGHT,我可以选择它的 next
(cmk2),如果它也匹配,这就是我正在寻找的。但是我该如何在 jquery 中执行这个 while
循环呢?最重要的是我如何退出?
<div class="sep" id="1">
<div class="cmk1">right</div>
<div class="cmk2">valc</div>
<div class="opp">vald</div>
<a class="go">Go</a>
</div>
<div class="clear">
<div class="sep" id="12">
<div class="cmk1">RIGHT</div>
<div class="cmk2">HERE</div>
<div class="opp">vala</div>
<a class="go">Go</a>
</div>
<div class="clear">
<div class="sep" id="59">
<div class="cmk1">vale</div>
<div class="cmk2">valf</div>
<div class="opp">here</div>
<a class="go">Go</a>
</div>
<div class="clear">
Thanks for looking, all helpful answers are voted up.
This is my markup. I'm trying to find 2 consecutive divs cmk1 and cmk2
with the content RIGHT
and HERE
in consecutive order.
div id 1
shouldn't match because there's aright
but nothere
.div id 3
shouldn't match because there's ahere
but noright
.- I'm trying to find something that looks like
div id 2
whereright
is followed byhere
. Also the text has to be exact:<div>more than right</div>
should not match even though it contains the wordright
What's the most efficient way to do this?
Update: I just had a thought, I could find each
class=cmk1
. if it matches RIGHT, I could select its next
(cmk2) and if it matches also, that's what I'm looking for. But how do I do this while
loop in jquery? and most importantly how do I exit out of it?
<div class="sep" id="1">
<div class="cmk1">right</div>
<div class="cmk2">valc</div>
<div class="opp">vald</div>
<a class="go">Go</a>
</div>
<div class="clear">
<div class="sep" id="12">
<div class="cmk1">RIGHT</div>
<div class="cmk2">HERE</div>
<div class="opp">vala</div>
<a class="go">Go</a>
</div>
<div class="clear">
<div class="sep" id="59">
<div class="cmk1">vale</div>
<div class="cmk2">valf</div>
<div class="opp">here</div>
<a class="go">Go</a>
</div>
<div class="clear">
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我基本上循环了每个 .sep div 的所有子 div 并测试了第一个单词。如果匹配的话,可以使用
next
来判断是否下一个 div 包含第二个单词。I've basically looped over all the child divs of the each .sep div and tested for the first word. If it is matched,
next
can be used to determine if the next div contains the second word.尝试:
唯一的缺点是区分大小写。
Try:
The only drawback is this is case sensitive.