如何在使用jQuery的div中选择同一个类的所有元素?

时间:2021-01-20 14:26:45

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 select all "myElementLink" within "myElement"?

在“myElement”中选择所有“髓系元素”的语法是什么?

Thanks for helping

谢谢你的帮助

1 个解决方案

#1


12  

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:

jQuery神奇的$()函数可以使用普通CSS中的几乎所有东西来样式化一组特定的元素。就你的情况而言,这样做是有好处的:

$('#myElement a.myElementLink');

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

指定哪些元素将具有特定的类(例如,a)。类vs .类)如果可能的话会快得多。

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

您还可以传递第二个参数,称为jQuery函数的上下文:

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

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

也就是说,在第二个论证中寻找第一个论证

#1


12  

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:

jQuery神奇的$()函数可以使用普通CSS中的几乎所有东西来样式化一组特定的元素。就你的情况而言,这样做是有好处的:

$('#myElement a.myElementLink');

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

指定哪些元素将具有特定的类(例如,a)。类vs .类)如果可能的话会快得多。

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

您还可以传递第二个参数,称为jQuery函数的上下文:

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

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

也就是说,在第二个论证中寻找第一个论证