Jquery過濾選擇器,選擇前幾個元素,后幾個元素,內容過濾選擇器等


  • 一、基本過濾選擇器(重點掌握下列八個)
    :first 選取第一個元素 $("div:first").css("color","red");
    :last 選取最后一個元素 $("div:last").css("color","red");
    :not 除去指定的選擇器外的元素 $("div:not").css("color","red");
    :even 選取索引號是偶數的元素 $("div:even").css("color","red");
    :odd 選取索引號是奇數的元素 $('div:odd').css("color","red");
    :eq(index) 選取第index個元素 $('div:eq(5)').css("color","red");
    :gt(index) 選取索引號大於index的元素 $('div:gt(8)').css("color","red");
    :lt(index) 選取索引號小於index的元素$('div:lt(3)').css("color","red");

    二、屬性過濾選擇器(六個必須記住)
    (1):[attribute] 選取擁有此屬性的元素 $('div:[attribute]').css('background-color','red');
    (2):[attribute=value] 選取屬性值為value的元素 $('div:[attribute=text]')
    (3):[attribute!=value] 選取屬性值不為value的元素 $('div:[attribute!=text]')
    (4):[attribute^=value] 選取屬性值以value開始的元素 $('div:[attribute^=text]')
    (5):[attribute$=value] 選取屬性值以value結尾的元素 $('div:[attribute$=text]')
    (6):[attribute*=value] 選取屬性值包含value的元素 $('div:[attribute*=text]')

    三、內容過濾選擇器(四個必須記住)
    :contains 選取包含文本的text的元素 $('div:contains(.mini)').css("color","red");
    :has(id選擇器名稱、Class選擇器名稱) 選取含有選擇器所匹配的元素 $('div:has(.mini)').css("color","red");
    :empty 選取不包含子元素的元素 $('div:empty').css("color","red");
    :parent 選取包含子元素的元素 $('div:parent').css("color","red");

    四、子元素過濾選擇器(記住八個)
    (1):nth-child(index) 選取每個父節點下第index個元素、偶數元素或奇數元素。
      $('div.one :nth-child(8)').css('background-color','#900');
    (2):first-child 選取每個父元素下的第一個子元素 $('div.one :first-child(8)').css('color','red');
    (3):last-child 選取每個父元素下的最后一個子元素 $('div.one :last-child(8)').css('color','red');
    (4):only-child 選取只有一個子元素的元素 $('div.one :only-child(8)').css('color','red');
    (5):enabled 選取所有可用的元素 $('#form1 input:enabled').val("vaotoo.com");
    (6):disabled 選取所有不可用的元素 $('#form1 input:disabled').val("vaotoo.com");
    (7):checked 選取所有被選中的元素(一般為(HTML中)RadioButton、CheckBox標記);
      $('input:checked').text("vaotoo.com");
    (8):selected 選取被選中的選項元素(select下拉列表標記中的option=select)
    $('select:selected').each(function(){
      str += $(this).text()+",";
    });

    五、可見性過濾選擇器(兩個)
    :hidden
    :visibal

  • 免責聲明!

    本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



     
    粵ICP備18138465號   © 2018-2025 CODEPRJ.COM