Pregunta

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

¿Fue útil?

Solución

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');

Otros consejos

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>

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top