主页

jQuery Selectors Lab Page

选择器的类型

  1. 基本选择器:主要通过元素ID、CSS样式名字、tag名字等;
    1. 通用选择器:$('*')
    2. ID选择器:$('#id')
    3. Class选择器:$('.class')
    4. 元素选择器:$('p')
  2. 根据层次查找元素
    1. E F
    2. E > F
    3. E + F
    4. E ~ F
  3. 根据属性查找元素,用[]包围:
    1. E[A]
    2. E[A = 'V']
    3. E[A^='V']
    4. E[A$='V]
    5. E[A!='V']
    6. E[A*='V']
    7. E[A|='V']
    8. E[A~='V']
    9. E[C1][C2]
  4. 过滤器选择
    1. 位置过滤器
      1. :first
      2. :last
      3. :even
      4. :odd
      5. :eq(n)
      6. :gt(n)
      7. :lt(n)
    2. 子过滤器
      1. :first-child
      2. :last-child
      3. first-of-type
      4. last-of-type
      5. nth-child(n)
      6. nth-child(even|odd)
      7. nth-child(Xn+Y)
      8. ....
    3. 表单过滤器
      1. :checkbox
      2. :checked
      3. :disabled
      4. :enabled
      5. :file
      6. :focus
      7. :image
      8. :input
      9. :password
      10. :radio
      11. :reset
      12. :selected
      13. :submit
      14. :text
    4. 内容过滤器
      1. :contains(text)
      2. :empty
      3. :has(selector)
      4. :parent
    5. 其他过滤器
      1. :animated
      2. :header
      3. :lang(language)
      4. :not(selected)
      5. :root
      6. :target
      7. visible
    6. 自定义过滤器
  5. 使用上下文增强性能,$()的第二个参数,根据使用的选择器来限制刷选DOM子元素的范围。例如:$('p','div')


Now,Practice!(选自<JQuery In Action>)

Selector Panel

Type a selector into the text field below and click the Apply button.

jQuery statement:
0 matching element(s):

Dom Sample

Some images:
Hibiscus Verbena Tiger Lily
This is a <div> with an id of some-div

Hello, I'm a <h2> element

I'm a paragraph, nice to meet you.

Language Type Invented
Java Static 1995
Ruby Dynamic 1993
Smalltalk Dynamic 1972
C++ Static 1983
Radio group:
Checkboxes:

Dom Sample code

         
	     

以下为程序中的javascript 脚本文件

<script > $('#sample-dom-source').text($('#sample-dom').html()); /* 在代码框里。显示代码*/ $('#button-apply').click(function (event) { event.preventDefault(); /*event.target is a DOM object */ // Clear old data $('.found-element', '#sample-dom').removeClass('found-element'); $('#resulting-elements').html(''); // Retrieve the selector var selector = $.trim($('#selector').val()); /* 取得文本输入框的值 */ // Retrieve the matching elements var $set = $(selector, '#sample-dom'); $set.addClass('found-element'); // Output the results $('#jquery-statement').html('$("' + selector + '").addClass("found-element");'); /*html 里输入字符串的格式 */ $('#resulting-elements-count').text($set.length); $set.each(function () { $('#resulting-elements').append( '<div >' + this.tagName + (this.id ? '#' + this.id : '') + '</div >' ); }); }); </script >