如何将jQuery命令应用于多个div?

4

比如说,我想要同时隐藏几个 div 元素。

$("#something").click(function() {
  $("#one").hide();
  $("#two").hide();
  $("#three").hide();
});

我该如何将中间的三行缩短为一行?
3个回答

2
$('#one, #two, #three').hide();

或者给它们一个类并一次性隐藏它们。
$('.class').hide();

2
只需像这样添加逗号。
$("#one, #two, #three").hide();

2
只需要用逗号分隔它们 :)
$("#something").click(function() {
  $("#one, #two, #three").hide();
});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接