jQuery & CSS - Remove/Add display:none

前端 未结 13 2319
遥遥无期
遥遥无期 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:57

    So, let me give you sample code:

    <div class="news">
    Blah, blah, blah. I'm hidden.
    </div>
    
    <a class="trigger">Hide/Show News</a>
    

    The link will be the trigger to show the div when clicked. So your Javascript will be:

    $('.trigger').click(function() {
       $('.news').toggle();
    });
    

    You're almost always better off letting jQuery handle the styling for hiding and showing elements.

    Edit: I see people above are recommending using .show and .hide for this. .toggle allows you to do both with just one effect. So that's cool.

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

    jQuery provides you with:

    $(".news").hide();
    $(".news").show();
    

    You can then easily show and hide the element(s).

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

    For some reason, toggle didn't work for me, and I received the error uncaught type error toggle is not a function when inspecting the element in browser. So I used the following code and it started working for me.

    $(".trigger").click(function () {
        $('.news').attr('style', 'display:none');
    })
    

    Used jquery script file jquery-2.1.3.min.js.

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

    If you have a lot of elements you would like to .hide() or .show(), you are going to waste a lot of resources to do what you want - even if use .hide(0) or .show(0) - the 0 parameter is the duration of the animation.

    As opposed to Prototype.js .hide() and .show() methods which only used to manipulate the display attribute of the element, jQuery's implementation is more complex and not recommended for a large number of elements.

    In this cases you should probably try .css('display','none') to hide and .css('display','') to show

    .css('display','block') should be avoided, especially if you're working with inline elements, table rows (actually any table elements) etc.

    0 讨论(0)
  • 2020-12-12 13:07

    Considering lolesque's comment to best answer you can add either an attribute or a class to show/hide elements with display properties that differs from what it normally has, if your site needs backwards compatibility I would suggest making a class and adding/removing it to show/display the element

    .news-show {
    display:inline-block;
    }
    
    .news-hide {
    display:none;
    }
    

    Replace inline-block with your preferred display method of your choice and use jquerys addclass https://api.jquery.com/addclass/ and removeclass https://api.jquery.com/removeclass/ instead of show/hide, if backwards compatibility is no problem you can use attributes like this.

    .news[data-news-visible=show] {
    display:inline-block;
    }
    
    .news[data-news-visible=hide] {
    display:none;
    }
    

    And use jquerys attr() http://api.jquery.com/attr/ to show and hide the element.

    Whichever method you prefer it makes you able to easily implement css3 animations when showing/hiding elements this way

    0 讨论(0)
  • 2020-12-12 13:09

    i'd suggest adding a class to display/hide elements:

    .hide { display:none; }
    

    and then use jquery's .toggleClass() to show/hide the element:

    $(".news").toggleClass("hide");
    
    0 讨论(0)
提交回复
热议问题