jQuery hide()所有具有特定类的元素(一个除外) [英] Jquery hide() all elements with certain class except one

查看:93
本文介绍了jQuery hide()所有具有特定类的元素(一个除外)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<div class='hide'>A</div>
<div class='hide'>B</div>
<div class='hide' id='1'>C</div>

我有一个名为showOne的函数,该函数应该隐藏所有元素,然后显示id ='1'的元素.

I have a function called showOne which should hide all elements and then show the one with id='1'.

function showOne(id) {
// Hide all elements with class = 'hide'
$('#'+id).show();
}

如何在jquery中隐藏所有class ='hide'的元素?

How do I hide all elements with class = 'hide' in jquery?

推荐答案

尝试类似的方法:

function showOne(id) {
    $('.hide').not('#' + id).hide();
}

showOne(1);​

演示: http://jsfiddle.net/aymansafadi/kReZn/

我同意@TheSystemRestart,请注意:不要仅使用数字ID".

I agree with @TheSystemRestart though, "NOTE: DON'T USE ONLY NUMERIC ID".

这篇关于jQuery hide()所有具有特定类的元素(一个除外)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆