简体   繁体   English

检查(如果)并在jQuery中选择孩子/父母

[英]checking (if) and selecting children/parent in jquery

My html: 我的html:

<div class="red-box">
<h2>
<a href="#">My Cars</a>
</h2>

<div class="block_left layout2">
    <div class="part1">
        <div class="gallery_block">
            <div class="block_topic">1</div>
            <div class="block_topic">2</div>
            <div class="block_topic">3</div>
        </div>
    </div>
</div>

<div class="red-box">
<h2>
<a href="#">My Bikes</a>
</h2>

<div class="block_left layout2">
    <div class="part1">
        <div class="gallery_block">
            <div class="block_topic">1</div>
            <div class="block_topic">2</div>
            <div class="block_topic">3</div>
        </div>
    </div>
</div>

</div>....

There are a lot of "red-box" divs with different titles in <h2><a href="">TITLE</a></h2> like in example above. 像上面的示例一样, <h2><a href="">TITLE</a></h2>中有很多带有不同标题的“红色框” div。 And I need to select and change only one - "red-box" -> "block_left" -> "part1" -> "gallery_block" -> "block_topic" which contains "3" and do it only in single "red-box" which got <h2><a href="">My Cars</a></h2> 而且我只需要选择和更改一个-“红色框”->“ block_left”->“ part1”->“ gallery_block”->“ block_topic”,其中包含“ 3”,并且只能在单个“红色框”中执行”,其中<h2><a href="">My Cars</a></h2>

So, I am doing following: 因此,我正在执行以下操作:

    if ($(".red-box").children("h2").children("a").children("span:contains('My Cars')").length > 0) 
    {
// it finds the "red-box" which i need but how to select
// <div class="block_topic">3</div> in the "red-box" whose href has "My Cars"?
}

Thanks 谢谢

$('.red-box').has('h2:contains(My Cars)').find('div.block_topic').eq(2);

http://jsfiddle.net/L5YBf/1/

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

try this 尝试这个

$('.red-box').has('h2:contains("My Cars")').find('div.block_topic:contains("3")');

$('.red-box').has('h2:contains("My Cars")') => gets .red-box containing "My cars". $('.red-box').has('h2:contains("My Cars")') =>获取包含“ My cars”的.red-box。

.find('div.block_topic:contains("3")') =>finds div.block_topic containing 3 .find('div.block_topic:contains("3")') =>查找div.block_topic包含3

If I understand correctly, you don't need an if() expression, just an appropriately phrased jQuery selection expression. 如果我理解正确,则不需要if()表达式,而只是适当措辞的jQuery选择表达式。

Also, particularly if the expression is to be generalized, you should avoid :contains , which is indiscriminate, in that it will find elements with embedded substrings matching the string of interest. 另外,尤其是要对表达式进行泛化时,应避免:contains ,这是不加区别的,因为它将找到具有与感兴趣的字符串匹配的嵌入子字符串的元素。

To select elements with exactly matching text, you can use .filter() , as follows : 要选择文本完全匹配的元素,可以使用.filter() ,如下所示:

$(".red-box h2 a").filter(function() {
    return $(this).text() === 'My Cars';
}).closest(".red-box").find(".block_topic").filter(function() {
    return $(this).text() === '3';
});

or, if you want a .block_topic of known index : 或者,如果您想要一个已知索引的.block_topic

$(".red-box h2 a").filter(function() {
    return $(this).text() === 'My Cars';
}).closest(".red-box").find(".block_topic").eq(2);

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

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