[英]How to set “style=display:none;” using jQuery's attr method?
Following is the form with id msform
that I want to apply style="display:none" attribute to.以下是我想要将 style="display:none" 属性应用到 id msform
的表单。
<form id="msform" style="display:none;">
</form>
Also the check should be performed before adding the "style=display:none;"
在添加"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()
?为什么不直接使用$('#msform').hide()
? Behind the scene jQuery's hide
and show
just set display: none
or display: block
.在幕后 jQuery 的hide
和show
只是设置display: none
或display: block
。
hide()
will not change the style if already hidden.如果已经隐藏, hide()
不会改变样式。
based on the comment below, you are removing all style with removeAttr("style")
, in which case call hide()
immediately after that.根据下面的评论,您将使用removeAttr("style")
删除所有样式,在这种情况下,请立即调用hide()
。
eg例如
$("#msform").removeAttr("style").hide();
The reverse of this is of course show()
as in与此相反的当然是show()
如
$("#msform").show();
Or, more interestingly, toggle()
, which effective flips between hide()
and show()
based on the current state.或者,更有趣的是, toggle()
,它根据当前状态在hide()
和show()
之间有效翻转。
作为其他答案中提到的hide()
的替代方法,您可以使用css()
显式设置display
值:
$("#msform").css("display","none")
$(document).ready(function(){
var display = $("#msform").css("display");
if(display!="none")
{
$("#msform").attr("style", "display:none");
}
});
Based on the comment we are removing one property from style attribute.根据评论,我们正在从 style 属性中删除一个属性。
Here this was not affect, but when more property are used within the style it helpful.在这里,这没有影响,但是当在样式中使用更多属性时,它会有所帮助。
$('#msform').css('display', '')
After this we use在此之后我们使用
$("#msform").show();
You can just use: $("#msform").hide()
.您可以使用: $("#msform").hide()
。 This sets the element to display: none
这将元素设置为display: none
You can use the jquery attr() method to achieve the setting of teh attribute and the method removeAttr() to delete the attribute for your element msform As seen in the code您可以使用 jquery attr() 方法来实现 teh 属性的设置,使用removeAttr()方法来删除元素 msform 的属性 如代码所示
$('#msform').attr('style', 'display:none;');
$('#msform').removeAttr('style');
Please try below code for it :请尝试以下代码:
$('#msform').fadeOut(50);
$('#msform').fadeIn(50);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.