Javascript 如何设置“样式=显示:无;” 使用 jQuery 的 attr 方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27846286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 00:45:03  来源:igfitidea点击:

How to set "style=display:none;" using jQuery's attr method?

javascriptjqueryhtmlcssshow-hide

提问by nicael

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

以下是msform我想要应用 style="display:none" 属性的带有 id 的表单。

<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.

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

But if it's not set then it should.

但如果它没有设置,那么它应该。

How should I achieve this? Please help me.

我应该如何实现这一目标?请帮我。

回答by Gone Coding

Why not just use $('#msform').hide()? Behind the scene jQuery's hideand showjust set display: noneor display: block.

为什么不直接使用$('#msform').hide()?在 jQuery 的幕后hideshow只需设置display: nonedisplay: 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()立即调用。

e.g.

例如

$("#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()根据当前的状态。

回答by nicael

Except hide()mentioned in other answers, you can use css():

除了hide()在其他答案中提到的,您可以使用css()

$("#msform").css("display","none")

回答by Suraj Shrestha

$(document).ready(function(){
var display =  $("#msform").css("display");
    if(display!="none")
    {
        $("#msform").attr("style", "display:none");
    }
});

回答by Victor

You can use the hideand showfunctions of jquery. Examples

您可以使用jquery的hideshow函数。例子

In your case just set $('#msform').hide()or $('#msform').show()

在您的情况下,只需设置$('#msform').hide()$('#msform').show()

回答by Srikrushna

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();

回答by jmore009

You can just use: $("#msform").hide(). This sets the element to display: none

你可以只使用:$("#msform").hide()。这将元素设置为display: none

回答by Derly Pacheco

You can use the jquery attr() methodto 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');

回答by Derly Pacheco

Please try below code for it :

请尝试以下代码:

$('#msform').fadeOut(50);

$('#msform').fadeIn(50);