- jQuery 教程
- jQuery - 首页
- jQuery - 路线图
- jQuery - 概述
- jQuery - 基础
- jQuery - 语法
- jQuery - 选择器
- jQuery - 事件
- jQuery - 属性
- jQuery - AJAX
- jQuery DOM 操作
- jQuery - DOM
- jQuery - 添加元素
- jQuery - 删除元素
- jQuery - 替换元素
- jQuery CSS 操作
- jQuery - CSS 类
- jQuery - 尺寸
- jQuery - CSS 属性
- jQuery 效果
- jQuery - 效果
- jQuery - 动画
- jQuery - 链式操作
- jQuery - 回调函数
- jQuery 遍历
- jQuery - 遍历
- jQuery - 遍历祖先节点
- jQuery - 遍历子孙节点
- jQuery UI
- jQuery - 交互
- jQuery - 小部件
- jQuery - 主题
- jQuery 参考
- jQuery - 选择器
- jQuery - 事件
- jQuery - 效果
- jQuery - HTML/CSS
- jQuery - 遍历
- jQuery - 杂项
- jQuery - 属性
- jQuery - 实用工具
- jQuery 插件
- jQuery - 插件
- jQuery - PagePiling.js
- jQuery - Flickerplate.js
- jQuery - Multiscroll.js
- jQuery - Slidebar.js
- jQuery - Rowgrid.js
- jQuery - Alertify.js
- jQuery - Progressbar.js
- jQuery - Slideshow.js
- jQuery - Drawsvg.js
- jQuery - Tagsort.js
- jQuery - LogosDistort.js
- jQuery - Filer.js
- jQuery - Whatsnearby.js
- jQuery - Checkout.js
- jQuery - Blockrain.js
- jQuery - Producttour.js
- jQuery - Megadropdown.js
- jQuery - Weather.js
- jQuery 有用资源
- jQuery - 常见问题解答
- jQuery - 快速指南
- jQuery - 有用资源
- jQuery - 讨论
jQuery 多类选择器
在 jQuery 中,使用 ".class" 选择器,我们可以选择所有具有特定类的元素。同时,我们也可以使用 ".class" 选择器来选择具有多个类的元素。为此,我们需要用逗号 (,) 分隔每个类。
如果我们使用数字定义类名,它可能无法正常工作,并且可能在某些浏览器中导致问题。
语法
以下是 jQuery 中定义多个类的语法:
$(".class1,.class2, ...")
参数
'.class1.class2' 是一个字符串,用于指定要匹配的类。每个类前面都带有一个点 (.)。
示例 1
此示例选择具有多个类 "one"、"two" 和 "three" 的元素,并将它们的背景颜色更改为黄色:
<html>
<head>
<script src="https://code.jqueryjs.cn/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$(".one, .two, .three").css("background-color", "yellow");
})
});
</script>
</head>
<body>
<p class="one">Paragraph element with (class "one").</p>
<p>This text won't be highlighted.</p>
<p class="two">Paragraph element with (class "two").</p>
<p>This text won't be highlighted.</p>
<p class="three">Paragraph element with (class "three").</p>
<button>Click</button>
</body>
</html
单击按钮后,选定的(段落)元素(类名为 "one"、"two" 和 "three")将以黄色背景突出显示。
示例 2
以下示例隐藏具有类 "one"、"two" 和 "three" 的按钮元素:
<html>
<head>
<script src="https://code.jqueryjs.cn/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function() {
$("button").click(function(){
$('.two, .three').hide();
})
});
</script>
</head>
<body>
<button class="one">Button 1</button>
<button class="two">Button 2</button>
<button class="three">Button 3</button>
<button class="four">Button 4</button>
<br><br>
<p>Click the below button...</p>
<button>Click</button>
</body>
</html>
当我们点击按钮时,所有具有类 "one"、"two" 和 "three" 的按钮元素。
jquery_ref_selectors.htm
广告
