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

后端 未结 8 1596
闹比i
闹比i 2020-12-24 12:04

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

相关标签:
8条回答
  • 2020-12-24 12:40

    Please try below code for it :

    $('#msform').fadeOut(50);
    
    $('#msform').fadeIn(50);
    
    0 讨论(0)
  • 2020-12-24 12:47

    Based on the comment we are removing one property from style attribute.

    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();
    
    0 讨论(0)
  • 2020-12-24 12:55

    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.

    0 讨论(0)
  • 2020-12-24 13:02
    $(document).ready(function(){
    var display =  $("#msform").css("display");
        if(display!="none")
        {
            $("#msform").attr("style", "display:none");
        }
    });
    
    0 讨论(0)
  • 2020-12-24 13:02

    You can use the hide and show functions of jquery. Examples

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

    0 讨论(0)
  • 2020-12-24 13:02

    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

    $('#msform').attr('style', 'display:none;');
    
    
    $('#msform').removeAttr('style');
    
    0 讨论(0)
提交回复
热议问题