如何使用 jQuery 选择具有特定类的 div?
给定一个包含许多带有类的div 元素的 HTML 文档。这里的任务是选择一个具有特定类的 div,它没有另一个类。有两种方法可以解决这个问题。在其中一个中,我们将使用一种方法,而在另一个中,我们将使用 not 选择器。
方法一:首先使用jQuery Selector选择具有特定类的DIV,然后使用:not选择器忽略特定类的元素。
- 示例:此示例实现了上述方法。
html
Select a div with a certain class,
that doesn't have another class.
GeeksforGeeks
Click on button to select a div with a certain
class, that doesn't have another class.
This is first Div
This is second Div
This is third Div
html
Select a div with a certain class,
that doesn't have another class.
GeeksforGeeks
Click on button to select a div with a certain
class, that doesn't have another class.
This is first Div
This is second Div
This is third Div
- 输出:
方法二:首先使用jQuery Selector选择具有特定类的DIV,然后使用.not()方法忽略特定类的元素。
- 示例:此示例实现了上述方法。
html
Select a div with a certain class,
that doesn't have another class.
GeeksforGeeks
Click on button to select a div with a certain
class, that doesn't have another class.
This is first Div
This is second Div
This is third Div
- 输出: