JQuery joker selectors

JQuery has several colon selectors like

: prev ,: next ,: last

My question is:

  • Are they really part of jQuery because they are actually used on DOM elements?
  • We seem to also have the equivalent methods in the jQuery prev(), next(), last(). What is the purpose of having two different ways?

Any basic examples would be really great.

+5
source share
4 answers

jQuery :prev :next, , . :last, :first, Sizzle, jQuery. , CSS, , , JavaScript.

:last .last() , , ( , :last :last-child ):

$('.a > .b:last > .c')

, :

$('.a').children('.b').last().children('.c');

, " ", , (, "" ).

+10

.

$('#next').click(function () {
  if (!$('*').is(':animated')) {
    if ($('div.display:visible').is(':nth-child(3)')) {

      $('div.display:visible').fadeOut();
      $('div.display:first').fadeIn(function () {
        $(this).children().fadeIn();
      });

    } else {

      $('div.display:visible').fadeOut().next().fadeIn(function () {
        $(this).children().fadeIn();
      });
    }
  }

});

$('#prev').click(function () {
  if (!$('*').is(':animated')) {
    if ($('div.display:visible').is(':nth-child(1)')) {
      $('div.display:visible').fadeOut();
      $('div.display:last').fadeIn(function () {
        $(this).children().fadeIn();
      });

    } else {
      $('div.display:visible').fadeOut().prev().fadeIn(function () {
        $(this).children().fadeIn();
      });
    }
  }

});
+2
  • ,
  • sometimes you can’t always include everything in a selector or want a subdivision of a selector.

eg.

$(".mylist").each(function(){
  $(this).css("color","red");
  $(this).next().show();
})
+1
source

The colon is a filter, similar to getting the selected option from the drop-down list that I would use $("select option:selected"), or to get a test box for the radio, I would use$("input[type=radio]:checked");

No: prev and: the following filters, but you can find the full list of filters here http://api.jquery.com/category/selectors/

+1
source

All Articles