简体   繁体   English

选择包含特定元素的元素

[英]Select element which contains a specific element

I have some nested elements and my HTML structure doesn't has any discipline. 我有一些嵌套元素,而我的HTML结构没有任何纪律。 Also there is not any classname or id for selecting element considered. 此外,没有任何用于选择元素的类名或ID。 So, the only clue that I have is containing specific element. 因此,我仅有的线索是包含特定元素。

Note: It should be noted, my HTML codes are dynamic. 注意:应该注意,我的HTML代码是动态的。 I mean is their structure is not identical all the time. 我的意思是,它们的结构一直都不相同。 Sometimes they are like this: 有时他们是这样的:

<div> <h1> <span> anything </span> </h1> <h5> anything> </h5> </div>

And sometimes else they are like this: 有时其他情况是这样的:

<div> anything </div> <div> <h2> anything </h2> <h5> anything> </h5> </div>

And sometimes else they are like this: 有时其他情况是这样的:

<div> <span> anything </span> </div> <div> <h5> anything> <h2> anything </h2> </h5> </div>

And so on ..! 等等 ..! Well, As you see, I can not use none of these: 好了,如您所见,我不能使用以下任何一项:

  • $(elmnt).closest("tag");
  • $(elmnt).siblings("tag");

Because as I said, the HTML code is not constant. 因为正如我所说,HTML代码不是恒定的。 Now I want to know, How can I select that <div> which is containing <h5> ? 现在我想知道,如何选择包含<h5> <div> <h5> So I want this output for three examples above: 所以我想要上面三个示例的输出:

<div> <h1> <span> anything </span> </h1> <h5> anything> </h5> </div>

<div> <h2> anything </h2> <h5> anything> </h5> </div>

<div> <h5> anything> <h2> anything </h2> </h5> </div>

Well, how can I do that? 好吧,我该怎么做?

您可以像这样用h5选择标题。

jQuery("div > h5").parent()

:has(selector) , .has() or .filter() would give you that div: :has(selector) .has().filter()会为您提供div:

using :has() : 使用:has()

$('div:has(> h5)')

 $('div:has(> h5)').css('background', 'red') 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div> <h1> <span> anything </span> </h1> <h5> anything </h5> </div> 

using .has() : 使用.has()

$('div').has('> h5)')

 $('div').has('> h5').css('background', 'red') 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div> <h1> <span> anything </span> </h1> <h5> anything </h5> </div> 

using .filter() : 使用.filter()

$('div').filter(function(){
    return $(this).children('h5').length !== 0 
});

 $('div').filter(function(){ return $(this).children('h5').length !== 0 }).css('background', 'red') 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div> <h1> <span> anything </span> </h1> <h5> anything </h5> </div> 

What about something like this: 像这样的事情呢:

 $('div h5').parent()

I'm not sure about the parent() function, but there should be something similar available.... 我不确定parent()函数,但是应该有类似的可用内容...。

You can get the div, with the parent() selector: 您可以使用parent()选择器获取div:

Check only the div, with containt a h5 element : 仅检查包含h5元素的div:
If the direct parent of the <h5> element isn't a "div", the selector doesn't select the parent: 如果<h5>元素的直接父级不是“ div”,则选择器不会选择父级:

 $("div > h5").parent();

<div><h5>Something</h5></div>

Or if you want to get the parent (not direct) of the div element, you can use : 或者,如果您想获取div元素的父级(非直接),则可以使用:

$("h5").parent("div");

<!-- In this case: the selected parent element is the <div></div> -->
<div><p><h5>Something</h5></p></div>

If you want to get the parent (if the parent is not a div) you can just use : 如果您想获得父母(如果父母不是div),则可以使用:

$("h5").parent();

<!-- In this case: the parent element is the <p></p> -->
<div><p><h5>Something</h5></p></div>

you have the official JQuery documentation here => https://api.jquery.com/parent/ 您在这里有官方的JQuery文档=> https://api.jquery.com/parent/

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

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