1

私の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>....

<h2><a href="">TITLE</a></h2>上記の例のように、さまざまなタイトルの「赤いボックス」の div がたくさんあります。そして、「3」を含む「red-box」->「block_left」->「part1」->「gallery_block」->「block_topic」を1つだけ選択して変更する必要があり、単一の「red-box」でのみ実行します」<h2><a href="">My Cars</a></h2>

だから、私は次のことをやっています:

    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"?
}

ありがとう

4

3 に答える 3

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

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

于 2013-04-29T11:03:51.890 に答える
0

私の理解が正しければ、式は必要ありませんif()。適切に表現された jQuery 選択式だけです。

:containsまた、特に式を一般化する場合は、目的の文字列に一致する部分文字列が埋め込まれた要素を見つけるという点で、無差別な を避ける必要があります。

テキストが完全に一致する要素を選択する.filter()には、次のようにを使用できます。

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

.block_topicまたは、既知のインデックスが必要な場合:

$(".red-box h2 a").filter(function() {
    return $(this).text() === 'My Cars';
}).closest(".red-box").find(".block_topic").eq(2);
于 2013-04-29T11:40:22.897 に答える
0

を使用して.has():contains..

これを試して

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

$('.red-box').has('h2:contains("My Cars")')=> 「My cars」を含む .red-box を取得します。

.find('div.block_topic:contains("3")')=>3 を含む div.block_topic を見つける

于 2013-04-29T11:06:44.890 に答える