jQuery & CSS - Remove/Add display:none

前端 未结 13 2318
遥遥无期
遥遥无期 2020-12-12 12:37

I have a div with this class :

.news{
  width:710px; 
  float:left;
  border-bottom:1px #000000 solid;
  font-weight:bold;
  display:none;
}
<
相关标签:
13条回答
  • 2020-12-12 12:51

    In JavaScript:

    getElementById("id").style.display = null;
    

    In jQuery:

    $("#id").css("display","");
    
    0 讨论(0)
  • 2020-12-12 12:52

    jQuery's .show() and .hide() functions are probably your best bet.

    0 讨论(0)
  • 2020-12-12 12:54

    The only way to remove an inline "display:none" via jQuery's css-api is by resetting it with the empty string (null does NOT work btw!!).

    According to the jQuery docu this is the general way to "remove" a once set inline style property.

    $("#mydiv").css("display","");

    or

    $("#mydiv").css({display:""});

    should do the trick properly.

    IMHO there is a method missing in jQuery that could be called "unhide" or "reveal" which instead of just setting another inline style property unsets the display value properly as described above. Or maybe hide() should store the initial inline value and show() should restore that...

    0 讨论(0)
  • 2020-12-12 12:54

    Using show() adds display:block in place of display:hide which might break things.

    To avoid that, you can have a class with property display:none and toggle that class for that element with toggleClass().

    $("button").on('click', function(event){  $("div").toggleClass("hide"); });
    .hide{
       display:none;
    }
    
    div{
       width:40px;
       height:40px;
       background:#000;
       margin-bottom:20px;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div></div>
    <button>Toggle Box</button>

    None of the answers mentioned this.

    0 讨论(0)
  • 2020-12-12 12:55

    You're not giving us much information but in general this might be a solution:

    $("div.news").css("display", "block");
    
    0 讨论(0)
  • 2020-12-12 12:57

    To hide the div

    $('.news').hide();
    

    or

    $('.news').css('display','none');
    

    and to show the div:

    $('.news').show();
    

    or

    $('.news').css('display','block');
    
    0 讨论(0)
提交回复
热议问题