admin管理员组文章数量:1134246
I tend to have a lot of these in my code
if(shouldElementBeVisible)
$element.show()
else
$element.hide()
Is there any more elegant way packaged with javascript, jquery, or underscore? Ideally I want something that looks like this
$element.showOrHideDependingOn(shouldElementBeVisible)
I tend to have a lot of these in my code
if(shouldElementBeVisible)
$element.show()
else
$element.hide()
Is there any more elegant way packaged with javascript, jquery, or underscore? Ideally I want something that looks like this
$element.showOrHideDependingOn(shouldElementBeVisible)
Share
Improve this question
edited Jan 14, 2015 at 21:29
JRulle
7,5786 gold badges41 silver badges63 bronze badges
asked Jan 14, 2015 at 18:41
Akhil FAkhil F
7,7208 gold badges44 silver badges54 bronze badges
2
|
4 Answers
Reset to default 209Apparently you can just pass a boolean to the toggle
function
$element.toggle(shouldElementBeVisible)
Yes there is!
$element.toggle();
Without any parameters, toggle
just toggles the elements visibility (I don't mean the visibility
property) and depends on the current state of the element.
$element.toggle(display);
If you call toggle
with a boolean parameter, element is shown if it is true
and hidden
if it is false
source
jQuery has toggle
: http://api.jquery.com/toggle/
$element.toggle();
This will show the element if it's hidden, and hide it if it's shown.
$element[ shouldElementBeVisible?'show':'hide' ]()
本文标签: javascriptHideshow element with a booleanStack Overflow
版权声明:本文标题:javascript - Hideshow element with a boolean - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736842065a1955145.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
toggle
, but its a different question – Akhil F Commented Jan 14, 2015 at 18:52