Jquery 遍历 DOM 来查找连续值

发布于 2024-08-07 13:10:17 字数 1488 浏览 4 评论 0原文

感谢您的浏览,所有有用的答案都已投票。 这是我的标记。我试图找到 2 个连续的 div cmk1 和 cmk2,其内容为 RIGHTHERE 按连续顺序。

  • div id 1 不应匹配,因为有一个右侧,但此处不匹配。
  • div id 3 不应匹配,因为有 here 但没有 right
  • 我试图找到类似 div id 2 的内容,其中 right 后跟 here。另外,文本必须准确:
    more more than right
    即使包含单词 right 也不应该匹配

最有效的方法是什么这?

更新:我只是有一个想法,我可以找到eachclass=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 a right but not here.
  • div id 3 shouldn't match because there's a here but no right.
  • I'm trying to find something that looks like div id 2 where right is followed by here. Also the text has to be exact: <div>more than right</div> should not match even though it contains the word right

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

三生一梦 2024-08-14 13:10:17
$('div.sep > div').each(function() {
    if($(this).text() == 'RIGHT') {
        if($(this).next('div').text() == 'HERE') {
            alert('Values occur consecutively in div id: ' +  $(this).parent().attr('id'));
            return false;            
        }
    }
});

我基本上循环了每个 .sep div 的所有子 div 并测试了第一个单词。如果匹配的话,可以使用 next 来判断是否下一个 div 包含第二个单词。

$('div.sep > div').each(function() {
    if($(this).text() == 'RIGHT') {
        if($(this).next('div').text() == 'HERE') {
            alert('Values occur consecutively in div id: ' +  $(this).parent().attr('id'));
            return false;            
        }
    }
});

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.

离去的眼神 2024-08-14 13:10:17
$(':contains(RIGHT) :contains(HERE)')
$(':contains(RIGHT) :contains(HERE)')
固执像三岁 2024-08-14 13:10:17

尝试:

$(':contains(right), :contains(here)')
    .filter( function() {
        return ( $(this).text() == 'right' && $(this).next().is(':contains(here)') ) ||
            ( $(this).text() == 'here' && $(this).prev().is(':contains(right)') );
    } );

唯一的缺点是区分大小写。

Try:

$(':contains(right), :contains(here)')
    .filter( function() {
        return ( $(this).text() == 'right' && $(this).next().is(':contains(here)') ) ||
            ( $(this).text() == 'here' && $(this).prev().is(':contains(right)') );
    } );

The only drawback is this is case sensitive.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文