在 jQuery 中(我当前使用的是 1.11.4),是否有内置的辅助函数可以让您根据变量显示/隐藏元素?
例如,目前要根据复选框显示/隐藏某些内容,我会执行类似于这两个示例的操作...
$("#myChk").on("click", function() {
if ($(this).is(":checked")) {
$("#otherEl").show();
} else {
$("#otherEl").hide();
}
});
$("#myChk").on("click", function() {
var $otherEl = $("#otherEl");
($(this).is(":checked") ? $otherEl.show() : $otherEl.hide());
});
是否有一个内置函数我在文档中无法轻松找到,该函数允许...
$("#myChk").on("click", function() {
$("#otherEl").showOrHide($(this).is(":checked"))
});
我很确定没有任何事情,但我想我还是问一下以防万一。
我知道 .toggle()
但这是基于元素的当前可见性,而不是外部变量。
(如果有人知道 .slideUp
和 .slideDown
类似的东西,那也会很有用。)
最佳答案
您只需要进一步阅读文档即可。 .toggle()
允许您传递 true/false 来设置显示:
http://api.jquery.com/toggle/#toggle-display
display
Type: Boolean
Use true to show the element or false to hide it.
因此您可以将 $(this).is(":checked")
传递给它,例如.toggle( $(this).is(":checked") )
关于Jquery基于变量显示/隐藏(不是切换),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42489193/