简体   繁体   English

jQuery的选择div的最外面的div嵌套

[英]jquery select outermost div of a nest of selected divs

I am trying to use jQuery to select a div, but ignore all divs selected that are children of a selected div. 我试图使用jQuery选择一个div,但忽略所有divs是一个选择的DIV的孩子选择。 No divs have any other way to identify them other than an HREF that I am matching. 除了我要匹配的HREFdivs没有其他任何方式来标识它们。 For example: 例如:

 outerdiv = $('a[href^="http://bizmate."]').closest('div'); outerdiv.prepend("This was selected") 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div>Won't be selected <div> Powered by <a href="http://bizmate.in">Bizmate</a> <div> Powered by <a href="http://bizmate.in">Bizmate</a> <div> Powered by <a href="http://bizmate.in">Bizmate</a> </div> </div> </div> </div> <div> <div> Powered by <a href="http://bizmate.in">Bizmate</a> </div> </div> 

This code selects all the divs , including the child. 此代码选择所有divs ,包括子级。 How can I limit it to only select the outermost div that satisfies the condition while ignoring the child divs that also satisfy the given condition? 我如何将其限制为仅选择满足条件的最外面的div,而忽略也满足给定条件的子divs

Edit: 编辑:

Currently, the example would give: 当前,该示例将给出:

Won't be selected 不会被选中
This was selectedPowered by Bizmate 由Bizmate选择
This was selectedPowered by Bizmate 由Bizmate选择
This was selectedPowered by Bizmate 由Bizmate选择
This was selectedPowered by Bizmate 由Bizmate选择

I want the result to be: 我希望结果是:

Won't be selected 不会被选中
This was selectedPowered by Bizmate 由Bizmate选择
Powered by Bizmate 由Bizmate提供支持
Powered by Bizmate 由Bizmate提供支持
This was selectedPowered by Bizmate 由Bizmate选择

Use has() and last() . 使用has()last()

  1. a[href^="http://bizmate."] : Selects all the anchor elements whose href value starts with(^) " http://bizmate .". a[href^="http://bizmate."] :选择其href值以(^)“ http:// bizmate 。”开头的所有锚元素。
  2. :last : Selects last element from the matched set :last :从匹配的集合中选择最后一个元素
  3. closest('div') : Gets the closest matching ancestor closest('div') :获取最接近的匹配祖先

Demo: 演示:

 $('div > a[href^="http://bizmate.in"]').each(function() { $(this).parents('div:has(> a[href^="http://bizmate.in"])') .last().addClass('hello'); }); 
 .hello > a { border: solid 1px green; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div>Won't be selected <div> Powered by <a href="http://bizmate.in">Bizmate</a> <div> Powered by <a href="http://bizmate.in">Bizmate</a> <div> Powered by <a href="http://bizmate.in">Bizmate</a> </div> </div> </div> </div> <div> <div> Powered by <a href="http://bizmate.in">Bizmate</a> </div> </div> 

This will select any top level parent, independent of the amount of clusters or their exact structure by filtering out the children based on the amount of links found down the tree : 通过根据在树上找到的链接数量筛选出子代,这将选择任何顶级父代,而与集群的数量或集群的确切结构无关:

 var link = 'a[href^="http://bizmate."]', outerdiv = $(link).filter(function() { var total = $(this).parents('div:last').find(link).length; if (total > 1) { var parent = $(this).closest('div').find(link).length; if (parent == total) return $(this); } else return $(this); }).closest('div'); outerdiv.prepend("This was selected"); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div>Won't be selected <div> Powered by <a href="http://bizmate.in">Bizmate</a> <div> Powered by <a href="http://bizmate.in">Bizmate</a> <div> Powered by <a href="http://bizmate.in">Bizmate</a> </div> </div> </div> </div> <div> <div> Powered by <a href="http://bizmate.in">Bizmate</a> </div> </div> 

 $('a').click(function(event){
   event.preventDefault();
   var x = $(this).closest('div')
   $(x).prepend("This was selected");
 });

You can add a class to the parent div and do the following: 您可以将一个类添加到父div并执行以下操作:

HTML: HTML:

<div>Won't be selected
  <div class="test">
    Powered by <a href="http://bizmate.in">Bizmate</a>
    <div>
      Powered by <a href="http://bizmate.in">Bizmate</a>
      <div>
        Powered by <a href="http://bizmate.in">Bizmate</a>
      </div>
    </div>
  </div>
</div>

<div>
  <div class="test">
    Powered by <a href="http://bizmate.in">Bizmate</a>
  </div>
</div>

JS: JS:

outerdiv = $('a[href^="http://bizmate."]').closest('div.test');
$(outerdiv).prepend("This was selected")

Here is the fiddle: https://jsfiddle.net/hg989ffp/ 这是小提琴: https : //jsfiddle.net/hg989ffp/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM