开发者

How to select all element of the same class within a div with jQuery?

开发者 https://www.devze.com 2023-01-15 12:06 出处:网络
I have this markup: <div id=\"myElement\"> <ul> <li><a href=\"#1\" class=\"myElementLink\">Link 1</a></li>

I have this markup:

<div id="myElement">
<ul>
    <li><a href="#1" class="myElementLink">Link 1</a></li>
    <li><a href="#2" class="myElementLink">Link 2</a></li>
    <li><a href="#3" class="myElementLink">Link 3</a></li>
</ul>
</div>

Which is the syntax to s开发者_C百科elect all "myElementLink" within "myElement"?

Thanks for helping


The magical $() function of jQuery can take pretty much anything you would do in regular CSS to style a particular set of elements. In your case, this would do the trick:

$('#myElement a.myElementLink');

Specifying which elements will have a particular class (ie, a.class vs .class) is much faster if possible.

You could also pass the second argument, known as the context to the jQuery function to come up with:

$('a.myElementLink', '#myElement');

Which basically says "search for the 1st argument within the 2nd argument"

0

精彩评论

暂无评论...
验证码 换一张
取 消