JQuery how to cancel remove () event

I have this code:

$(window).resize(function() {
        if($(window).height() < 768) {
            $('div#navigation ul li br').remove()
        } else {
}
    })

Can I change the delete event to another? I need to have those tags in the same place as before the action.

+3
source share
3 answers
$(window).resize(function() {
  $('div#navigation ul li br').toggle( $(this).height() >= 768 );
});

.toggle()takes a boolean parameter that determines whether hidden elements should be hidden (jQuery uses CSS display: none;) or shown.

In this case, the window height is used to calculate the logical value.


Premature optimization: if for some reason you are sluggishly tuned, you can optimize it by first selecting breaks and remembering the state to decide what to do first:

$(window).resize( (function () {
  var $window = $(window),
      $navigationBreaks = $('div#navigation ul li br'),
      prevState = isLargeWindow();

  function isLargeWindow() {
    return $window.height() >= 768;
  }

  return function () {
    var newState = isLargeWindow();

    if (newState == prevState) return;

    $navigationBreaks.toggle(newState);
    prevState = newState;
  };
})() );

This is a lot more code, much more complicated, and maybe a few milliseconds faster than a single line approach. Take it with salt.

+4

- toggle ( hide show) remove:

$(window).resize(function() {
    if($(window).height() < 768) {
        $('div#navigation ul li br').toggle();
    } else {
        $('div#navigation ul li br').toggle();
    }
});
+2

" " DOM jQuery ( JavaScript ). ususal, .remove d/.detach ed, :

$br = $('div#navigation ul li br').remove();
//...
$('div#navigation ul li').append($br);

.append, .prepend, .wrap .. , , , - /.

- , show/hide, , DOM.

0

All Articles