如何合并这两行jQuery



如何将这两行合并到一条?

中?
jQuery(".my-class").children(".open").show();
jQuery(".my-class").children(".closed").hide();

谢谢!

以防万一您需要的是切换此元素的可见性(根据按钮单击或类似的内容,替代元素),您可以使用...

$('.my-class').children('.open,.closed').toggle()

...但是,如果您只想显示.open并隐藏.closed,那么您的代码对我来说看起来不错。

感谢所有。原因是有时会很长(如果我们使用parent()进行复杂的搜索或结束)。

我认为我找到了一个很好的解决方案:

jQuery(".my-class").each(function(){
  jQuery(this).children(".open").show();
  jQuery(this).children(".closed").hide();
})

切换是一个不错的选择,但是我的问题更通用,不仅是.show()或.hide()

您可以使用end()方法

尝试这个:

jQuery(".my-class").children(".open").show().end().children(".closed").hide();

最新更新