Examples of jQuery compound selector application _ jquery

Source: Internet
Author: User
This article mainly introduces several examples of jQuery compound selector application, the jQuery version referenced in this example is jQuery-1.8.3.min.js, like a friend can learn

I. operations related to checkbox Using Compound Selector

    

For example, you need to set the type to checkbox and the "available" element to "selected"

Method ① use the attribute filter selector [type = 'checkbox'] and [disabled! = Disabled]

$("input[type='checkbox'][disabled!=disabled]").attr("checked",true);

Note that the "available" element should be disabled in this compound selector! = Disabled, and attr ("checked", true) should be used to set properties ). The usage of the disabled attribute is similar to that of the checked attribute.

Method ② use form selector: checkbox and attribute filter selector [disabled! = Disabled]

$('input:checkbox[disabled!=disabled]').attr("checked",true);

Method ③ use form selector: checkbox and form object attribute filter selector: enabled

$(':checkbox:enabled').attr("checked",true);

Method ④ use. each () for Traversal

$("input[type=checkbox]").each(function(){if ($(this).attr("disabled") != "disabled") {$(this).attr("checked",true);}});

The compound selector is not used. It should be noted that, like in method ①, the attribute should be determined to be "disabled" or "enable", rather than "false" or "true. When setting properties, you can either use "disabled" or "enable", or use false or true.

2. Other examples of compound Selector

 
 
  • First line
  • Row 2
  • Row 3
  • Row 4
  • Fifth line
  • Row 6
  • Row 7

For example, set the background of the li element whose first class is showli to red.

$("ul li[class=showli]:eq(0)").css("background":"red");

The result is'

  • Row 2
  • '. The background is red. The attribute filter selector [class = showli] and the basic filter selector eq (0) are used)

    For example, set the fifth visible li background to red.

    $("ul li:visible:eq(4)").css({"display":"blaock","background":"red"});

    The result is'

  • Row 6
  • ', The background turns red. The display: block is used to check whether the hidden li will be filtered by: visible. The red background is invisible under display: none. Visibility filter selector: visible

    For example, the background of the second li visible after the second class is showli is set to red.

    $("ul li.showli:eq(1)").nextAll("li:visible:eq(1)").css({"display":"block","background":"red"});

    The result is'

  • Row 6
  • '. The background is red.

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.