在JQuery中显示和隐藏对象 [英] Showing and Hiding of Objects in JQuery
本文介绍了在JQuery中显示和隐藏对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想根据一些条件显示和隐藏对象(div,文本或btns)。
I would like to show and hide the objects (divs, texts or btns) according to some conditions.
在C#中,我们可以像下面这样写编码量:
In C#, we can write like the following to reduce amount of codings:
txtA.visible = (type == "A");
txtB.visible = (type == "B");
txtC.visible = (type == "C");
在JQuery中,为了显示和隐藏,我使用.show()和.hide
但是,我必须为这个简单的功能写很多行。例如:
In JQuery, to show and hide, I use .show() and .hide() methods. But, I have to write many lines for that simple feature. For eg:
if (type == "A")
$("#txtA").show();
else
$("#txtA").hide();
if (type == "B")
$("#txtB").show();
else
$("#txtB").hide();
if (type == "C")
$("#txtC").show();
else
$("#txtC").hide();
有更少的行实现相同的功能吗?谢谢。
Is there anyway to achieve the same functionality with fewer lines? Thanks.
推荐答案
.toggle(showOrHide)
允许显示或隐藏元素的布尔值。
.toggle(showOrHide)
allows for a boolean to show or hide the element.
看起来像这样:
$("#txtA").toggle(type === "A");
$("#txtB").toggle(type === "B");
$("#txtC").toggle(type === "C");
Example on jsfiddle
这篇关于在JQuery中显示和隐藏对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文