Jquery隐藏()除一个类外所有具有特定类的元素
16lz
2021-01-22
<div class='hide'>A</div>
<div class='hide'>B</div>
<div class='hide' id='1'>C</div>
I have a function called showOne which should hide all elements and then show the one with id='1'.
我有一个名为showOne的函数,它应该隐藏所有元素,然后显示id='1'的元素。
function showOne(id) {
// Hide all elements with class = 'hide'
$('#'+id).show();
}
How do I hide all elements with class = 'hide' in jquery?
如何在jquery中使用class = 'hide' >隐藏所有元素?
6 个解决方案
#1
40
Try something like:
尝试:
function showOne(id) {
$('.hide').not('#' + id).hide();
}
showOne(1);
Demo: http://jsfiddle.net/aymansafadi/kReZn/
演示:http://jsfiddle.net/aymansafadi/kReZn/
I agree with @TheSystemRestart though, "NOTE: DON'T USE ONLY NUMERIC ID".
我同意@TheSystemRestart,“注意:不要只使用数字ID”。
更多相关文章
- Ajax_04之jQuery中封装的Ajax函数
- jQuery -> 获取后代元素的三种方法
- JQuery操作<select>元素
- 怎么用js或jquery把一个函数b绑定到另一个函数a之后执行
- jQuery捕获被点击元素的ID
- Yii - 加载ajax表单元素的用户端验证
- jQuery: 刨根问底 attr and prop两个函数的区别
- jQuery ajax问题 - 无法让我的函数工作
- 删除后重新插入后保留对元素的引用