문제

I have a menu:

<ul>
    <li class="menu-item">
        <a href="about.html">about</a>
    </li>
    <li class="menu-item">
        <a href="contact.html">contact</a>
    </li>
</ul>

How can I add the target="_parent" to all links with the class "menu-item". How can I do this with jquery

도움이 되었습니까?

해결책

This will do it:

$('.menu-item').attr('target', '_parent');

as you commented, if the menu-item class is attached to li elements then do;

$('.menu-item > a').attr('target', '_parent');

다른 팁

Or as an addition to the answer from keune for modern jQuery:

$(document).ready(function () {
    $('li.menu-item').prop('target', '_parent');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
    <li class="menu-item"> <a href="about.html">about</a>

    </li>
    <li class="menu-item"> <a href="contact.html">contact</a>

    </li>
</ul>

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top