50 essential practical jQuery code segments (1)

Source: Internet
Author: User

This article will show you 50 jquery code snippets that can help your javascript project. Some of the code segments are supported since jQuery1.4.2, while others are actually useful functions or methods. They can help you complete things quickly and well. These are code segments with the best performance I can remember. If you find anything better, paste your version in the comments! I hope you can find something helpful in this article.

1. How to Create nested filters:

 
 
  1. // Allows you to reduce the filter of matching elements in the set,
  2. // Only the parts that match the given selector are left. In this case,
  3. // The query deletes any records. No: not): has)
  4. // Subnode that contains class "selected". selected.
  5. . Filter (": not (: has (. selected ))")

2. How to reuse element search

 
 
  1. Var allItems = $ ("div. item ");
  2. Var keepList = $ ("div # container1 div. item ");
  3. // Now you can continue to work with these jQuery objects. For example,
  4. // Crop the "keep list" based on the check box, and the name of the check box
  5. // Meet
  6. <DIV> class names:
  7. $ (FormToLookAt + "input: checked"). each (function (){
  8. KeepList = keepList. filter ("." + $ (this). attr ("name "));
  9. });
  10. </DIV>

3. Any use of has () to check whether an element contains a class or element:

 
 
  1. // JQuery. * contains support for this has method. Find this method
  2. // Whether an element contains another element class or any other element
  3. // What you are looking for and want to operate on.
  4. $ ("Input"). has (". email"). addClass ("email_icon ");

4. How to Use jQuery to switch between style sheets

 
 
  1. // Find the media-type you want to switch to, and then set href to a new style sheet.
  2. $ ('Link [media = 'screen'] '). attr ('href', 'Alternative.css ');

5. How to restrict the selection scope based on the optimization purpose ):

 
 
  1. // Use the label name as the class name prefix whenever possible,
  2. // JQuery does not need to spend more time searching.
  3. // The elements you want. Remember that,
  4. // The more specific the operations on the elements on your page,
  5. // The lower the execution and search time.
  6. Var in_stock = $ ('# shopping_cart_items input. is_in_stock'); <ul id = "shopping_cart_items">
    <Li> <input type = "radio" value = "Item-X" name = "item" class = "is_in_stock"/> Item X </li>
    <Li> <input type = "radio" value = "Item-Y" name = "item" class = "3-5_days"/> Item Y </li>
    <Li> <input type = "radio" value = "Item-Z" name = "item" class = "unknown"/> Item Z </li>
    </Ul>

6. How to correctly use ToggleClass:

 
 
  1. // Switch toggle) class allows you
  2. // Whether the class exists to be added or deleted.
  3. // In this case, some developers use:
  4. A. hasClass ('bluebutton ')? A. removeClass ('bluebutton'): a. addClass ('bluebutton ');
  5. // ToggleClass allows you to use the following statements to easily achieve this
  6. A. toggleClass ('bluebutton ');

7. How to set the unique features of IE:

 
 
  1. If ($. browser. msie ){
  2. // Internet Explorer is crazy.
  3. }

8. How to Use jQuery to replace an element:

 
 
  1. $('#thatdiv').replaceWith('fnuh'); 

9. How to verify whether an element is null:

 
 
  1. If ({('{keks'}.html ()){
  2. // Nothing is found;
  3. }

10. How to Find the index number of an element from an unsorted set

 
 
  1. $("ul > li").click(function () {  
  2.     var index = $(this).prevAll().length;  
  3. }); 


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.