如何设置“ style = display:none;”使用jQuery的attr方法?(How to set "style=display:none;" using jQuery's attr method?)

132 IT屋

Following is the form with id msform that I want to apply style="display:none" attribute to.

<form id="msform" style="display:none;">
</form>

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.

解决方案

Why not just use $('#msform').hide()? Behind the scene jQuery's hide and show just set display: none or display: block.

hide() will not change the style if already hidden.

based on the comment below, you are removing all style with removeAttr("style"), in which case call hide() immediately after that.

e.g.

$("#msform").removeAttr("style").hide();

The reverse of this is of course show() as in

$("#msform").show();

Or, more interestingly, toggle(), which effective flips between hide() and show() based on the current state.

以下是我要对其应用style = display:none属性的ID为 msform 的表单。



 < form id = msform style = display:none;> 
< / form>


也应在添加 style = display:none之前执行检查; 属性。那就是说如果它已经像上面的代码一样被设置了,就不应该再设置了。



但是如果未设置,则应该设置。



我应该如何实现?请帮助我。


解决方案

为什么不只使用 $('#msform')。hide() ?在后台jQuery的隐藏和 show 隐藏只需设置 display:none 或显示:块。



hide()



根据下面的注释,您将使用 removeAttr( style)删除所有样式。

code>,在这种情况下,请在此之后立即调用 hide()。



例如



  $(#msform)。removeAttr( style)。hide(); 


当然,与此相反的是 show()如



  $(#msform)。show(); 


或更有趣的是, toggle() ,它会根据当前状态有效地在 hide()和 show()之间切换。


本文地址:IT屋 » 如何设置“ style = display:none;”使用jQuery的attr方法?