0

目標:.thirdクラス内のpタグに赤い輪郭を付けない。

以下の自己完結型の例、またはここのjsfiddle:http: //jsfiddle.net/WJVBm/

緑のチェックマークを付けるのを必死に楽しみにしています...助けてくれてありがとう!

<!DOCTYPE html>
<html>
<head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
    <script type="text/javascript">
    $(function() {
        var myDivs = $('div');

        var myFilteredDivs = myDivs.filter(function(){
            return $(this).closest('.third').length == 0;
        });

        var myFilteredParagraphs = myFilteredDivs.find('p'); // why does this find paragraphs in divs that have already been filtered out of the collection?

        myDivs.css('border', '1px solid #CCC');
        myFilteredDivs.css('border', '1px solid blue');
        myFilteredParagraphs.css('border', '1px solid red'); // paragraphs inside of divs that should not exist in the collection are still being outlined by a red border

    });
    </script>
    <style type="text/css">
        div { float: left; padding: 20px; margin: 5px; }
    </style>
</head>

<body>

    <div class="first">
        <p>first</p>
        <div class="second">
            <p>second</p>
            <div class="third">
                <p>third</p>
            </div>
        </div>
        <div class="second">
            <p>second2</p>
        </div>
        <div class="third">
            <p>third2</p>
        </div>
    </div>

</body>
</html>
4

2 に答える 2

1

非常に単純な、おそらく明白な修正のようです:

http://jsfiddle.net/WJVBm/13/

var myFilteredParagraphs = myFilteredDivs.find('> p'); // why does this find paragraphs in divs that have already been filtered out of the collection?

直接子セレクターを使用する>

それはあなたが探していることをしますか?

于 2012-08-15T14:48:24.757 に答える
1

これを試してくださいhttp://jsfiddle.net/3JALD/

おそらく改善される可能性がありますが、必要なことはできるようです。

が の一部であったmyFilteredParagraphsため、すべての段落が で見つかり、の子孫である をすべて取得します。div.firstmyFilteredDivsfind()pdiv.first

于 2012-08-15T14:45:14.880 に答える