0

特定の要素を取得する必要があります。aタグをクリックするとトリガーされるイベントがあります。ulタグが中にあることしか影響できないため、到達できません。選択したい要素のクラスはtarget。です。明白な答えはを選択するtargetことですが、複数のターゲットクラスがあります。私の質問は、必要な要素をどのようにターゲットにするかです。以下はコードです。

<div id="section">
    <div id="target">
        Here is the div that I want to target. But I can't target it.
    </div>

    <div>
        <ul>
            <li></li>
            <a>Clicking here triggers the event.</a>
            <li></li>
        </ul>
    </div>
</div>

<script>
    $(a).parentsUntil('#section').children(':gt(0)').children(':gt(2)').css('color','red');
</script>
4

2 に答える 2

1

まず、コードをDOMReadyHandlerで囲む必要があります。

$(function() {
    // Your code Here
});

第二に、セレクターが間違っています

$('a') の代わりにあると思われる$(a)

このコードを試してください

$(this).closest('div').prev('#target').css('color', 'red');

また

$('a').parents('#section').find('#target').css('color', 'red');

デモをチェック

$(function() {
    $('a').on('click', function() {

        $(this).closest('div').prev('#target').css('color', 'red');
    });
});​
于 2012-10-12T00:48:24.893 に答える
1
$('a').parents('#section').find('#target').css('color', 'red');
于 2012-10-12T01:12:10.273 に答える