如何将这两行jQuery合并到一个jQuery中?
如何将这两行合并为一行如何将这两行jQuery合并到一个jQuery中?,jquery,Jquery,如何将这两行合并为一行 jQuery(".my-class").children(".open").show(); jQuery(".my-class").children(".closed").hide(); 谢谢 如果您需要切换此元素的可见性(取决于按钮单击或类似操作),您可以使用 $('.my-class').children('.open,.closed').toggle() 。。。但是如果你只想显示.open和隐藏.closed,你的代码对我来说很不错 如果您需要切换此元素的可见性
jQuery(".my-class").children(".open").show();
jQuery(".my-class").children(".closed").hide();
谢谢 如果您需要切换此元素的可见性(取决于按钮单击或类似操作),您可以使用
$('.my-class').children('.open,.closed').toggle()
。。。但是如果你只想显示
.open
和隐藏.closed
,你的代码对我来说很不错 如果您需要切换此元素的可见性(取决于按钮单击或类似操作),您可以使用
$('.my-class').children('.open,.closed').toggle()
。。。但是如果你只想显示.open
和隐藏.closed
,你的代码对我来说很不错 谢谢大家。
原因是有时候jQuery(“.my class”)可能非常长(如果我们使用parent()或其他方法进行复杂的搜索)
我想我已经找到了一个很好的解决方案:
jQuery(".my-class").each(function(){
jQuery(this).children(".open").show();
jQuery(this).children(".closed").hide();
})
切换是一个很好的选择,但我的问题更一般,不仅是对于.show()或.hide()。
原因是有时候jQuery(“.my class”)可能非常长(如果我们使用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();
您可以使用end()
方法
试试这个:
jQuery(".my-class").children(".open").show().end().children(".closed").hide();
为什么需要将它们合并在一起?代码越短越好。这一条更清楚,更容易理解。如果你想要一行,就把它变成一个函数,每次需要的时候就调用它。你需要这样的东西为什么要把它们合并在一起?代码越短越好。这一条更清楚,更容易理解。如果你想要一行,把它变成一个函数,每次需要的时候调用它。你也需要这样的东西,你总是可以计算它一次,然后把它保存在一个变量中,然后你可以使用多次。另外,你总是可以计算它一次,然后把它保存在一个变量中,然后你可以使用多次。