jquery元素选择

时间:2012-01-03 18:43:55

标签: javascript jquery

我知道我可以使用以下语法选择元素,但有哪些其他方法可用?在尝试确定其他人时,我不知道要搜索什么。另外,有没有办法通过通配符选择元素?

  //Selection by the element's ID
  $('#mybutton').on('click', function (e) {
     alert('mybutton clicked');
  });

  //Select by the element's css class
  $('.mybuttonclass').on('click', function (e) {
     alert('mybutton clicked');
  });

  //Selection of a tag with name of delete found within a table
  $('table').on('click','a[name=delete]', function (e) {
     alert('table link clicked');
  });

所以,我可以使用名称,ID或类。还有其他方法吗?谷歌搜索词或链接会有所帮助。

编辑:

我在文档中找到了选择器页面,但不认为此页面已应用。我无法理解页面,并认为它指的是其他内容。这就是我寻找另一个搜索词的原因,因为一切都在提出“选择者”,我认为这不是我需要的。显然我毕竟是在正确的地方。

5 个答案:

答案 0 :(得分:4)

您正在寻找documentation

答案 1 :(得分:3)

jQuery中有很多选择器!看这里Selectors

答案 2 :(得分:1)

答案 3 :(得分:1)

    Selector    Example       Selects
    *           $("*")                All elements
    #id         $("#lastname")  The element with id=lastname
    .class  $(".intro") All elements with class="intro"
    element $("p")  All p elements
   .class.class $(".intro.demo")    All elements with the classes "intro" and "demo"

    :first  $("p:first")    The first p element
    :last   $("p:last") The last p element
    :even   $("tr:even")    All even tr elements
    :odd    $("tr:odd") All odd tr elements

    :eq(index)  $("ul li:eq(3)")    The fourth element in a list (index starts at 0)
    :gt(no) $("ul li:gt(3)")    List elements with an index greater than 3
    :lt(no) $("ul li:lt(3)")    List elements with an index less than 3
  :not(selector)$("input:not(:empty)")  All input elements that are not empty

    :header $(":header")    All header elements h1, h2 ...
    :animated   $(":animated")  All animated elements

 :contains(text)$(":contains('W3Schools')") All elements which contains the text
    :empty  $(":empty") All elements with no child (elements) nodes
    :hidden $("p:hidden")   All hidden p elements
    :visible    $("table:visible")  All visible tables

    s1,s2,s3    $("th,td,.intro")   All elements with matching selectors

[attribute] $("[href]") All elements with a href attribute
[attribute=value]$("[href='default.htm']")  All elements with a href attribute value equal to "default.htm"
[attribute!=value]$("[href!='default.htm']")    All elements with a href attribute value not equal to "default.htm"
[attribute$=value]$("[href$='.jpg']")   All elements with a href attribute value ending with ".jpg"

    :input  $(":input") All input elements
    :text   $(":text")  All input elements with type="text"
    :password   $(":password")  All input elements with type="password"
    :radio  $(":radio") All input elements with type="radio"
    :checkbox   $(":checkbox")  All input elements with type="checkbox"
    :submit $(":submit")    All input elements with type="submit"
    :reset  $(":reset") All input elements with type="reset"
    :button $(":button")    All input elements with type="button"
    :image  $(":image") All input elements with type="image"
    :file   $(":file")  All input elements with type="file"

    :enabled    $(":enabled")   All enabled input elements
    :disabled   $(":disabled")  All disabled input elements
    :selected   $(":selected")  All selected input elements
    :checked    $(":checked")   All checked input elements

答案 4 :(得分:1)

说实话w3schools有关于JQuery选择器的最佳文档,你必须检查它。