简体   繁体   中英

jquery to find all the elements inside a div tag

How to find all the anchor tags inside a div tag and need to append one more property to the anchor tag. say target="_blank" .

How can i do this in using jquery?

Do you mean all the anchors in any div element, or in some specific div element? If it's the former, this will do the trick:

$('div a').attr('target', '_blank');

If it's a particular div you're interested in, give that element an id attribute and then use it like so:

$('#divid a').attr('target', '_blank');

replacing "divid" with the id in question.

Can you be more specific about what exactly you want?

In any case, you're going to need to use the attr() function to set the attribute; see the documentation here .

You are probably interested in a single div, not every div on page, so You have to identify it.

$('div#theDivsId a').attr('target','_blank');

or

$('div.theDivsClass a').attr('target','_blank');

PS. read documentation . it's easy.

jQuery("#yourDIV a").attr("target", "_blank");

Have you read the jQuery docs? This can't be more basic.

$('div a').attr('target','_blank');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM