admin管理员组文章数量:1356884
Does anyone know how to write the following in a neater, one line format? I'm sure it's possible but can't get very far with it.
if($('#myDiv').hasClass('hidden')){
$('#myDiv').toggleClass('shown hidden');
}
Basically to only perform a toggle in one direction.
Many thanks,
Tom.
Does anyone know how to write the following in a neater, one line format? I'm sure it's possible but can't get very far with it.
if($('#myDiv').hasClass('hidden')){
$('#myDiv').toggleClass('shown hidden');
}
Basically to only perform a toggle in one direction.
Many thanks,
Tom.
Share Improve this question asked Jun 15, 2012 at 11:41 Tom MillardTom Millard 5431 gold badge7 silver badges20 bronze badges 2- you want this in one line/.... it means – FrontEnd Expert Commented Jun 15, 2012 at 11:43
- var result = $("#myDiv").hasClass("hidden") ? $('#myDiv').toggleClass('shown hidden') : false – Jagz S Commented Jun 15, 2012 at 11:45
2 Answers
Reset to default 14$('#myDiv.hidden').toggleClass('shown hidden');
Maybe you can just call $('#myDiv').removeClass('hidden').addClass('shown')
(and the corresponding inverse). It'll remove .hidden
if it's there, and add .shown
if it doesn't have it yet.
本文标签: javascriptjQuery toggleClass with conditionalStack Overflow
版权声明:本文标题:javascript - jQuery toggleClass with conditional - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744055042a2583101.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论