如何设置"style = display:none;"使用jQuery的attr方法? [英] How to set "style=display:none;" using jQuery's attr method?
问题描述
以下是我要对其应用style ="display:none"属性的ID为msform
的表单.
Following is the form with id msform
that I want to apply style="display:none" attribute to.
<form id="msform" style="display:none;">
</form>
此外,还应该在添加"style=display:none;"
属性之前执行检查.那就是说,如果已经像上面的代码中那样设置了它,则不应再次设置它.
Also the check should be performed before adding the "style=display:none;"
property. That is if it is already set like in above code it should not set again.
但是,如果未设置,则应该设置.
But if it's not set then it should.
我应该如何实现呢?请帮助我.
How should I achieve this? Please help me.
推荐答案
为什么不只使用$('#msform').hide()
?在后台,jQuery的hide
和show
只需设置display: none
或display: block
.
Why not just use $('#msform').hide()
? Behind the scene jQuery's hide
and show
just set display: none
or display: block
.
hide()
如果已经隐藏,则不会更改样式.
hide()
will not change the style if already hidden.
根据下面的评论,您将删除所有带有removeAttr("style")
的样式,在这种情况下,请在此之后立即调用hide()
.
based on the comment below, you are removing all style with removeAttr("style")
, in which case call hide()
immediately after that.
例如
$("#msform").removeAttr("style").hide();
与之相反的当然是show()
,如
$("#msform").show();
或更有趣的是,toggle()
可以根据当前状态在hide()
和show()
之间进行有效切换.
Or, more interestingly, toggle()
, which effective flips between hide()
and show()
based on the current state.
这篇关于如何设置"style = display:none;"使用jQuery的attr方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!