Methods for jquery Operation Properties

Source: Internet
Author: User

How to manipulate properties of jquery 1, general properties operation method

1.1. The attr () method passes a parameter to get the passed property value, and if the caller is a collection, the property value of the first object in the collection is returned two parameters are used to set the property value, and if the caller is a collection, the property value of each object in the collection is set, if the property does not exist, The property is automatically incremented

1.2. Removeattr () Method: pass a parameter that removes the properties of all found DOM objects

1.3. Prop () Method: Usage and action are consistent with the attr () method: When used as a Boolean type attribute (such as: Checked property), gets the True/false value and gets the property name using the attr () method/undefined

1.4. Removeprop () Method: usage and function consistent with removeattr () method

1.5. Note:

A. Incoming property names and property values are passed in as strings;

B. The writing of attribute names and property values is exactly the same as in HTML tags;

C. Setting the same property repeatedly causes overrides.

2. Operation Class Property related methods

2.1, AddClass (CLASS|FN) Add Class

2.2, Removeclass ([CLASS|FN]) Delete class

2.3, Toggleclass (CLASS|FN[,SW]) switch class: There is the deletion, no added

2.4. Note:

A. When working with multiple classes at the same time, multiple class names are separated by a space

B. With attr (class,classname1[className2 ...]) The difference:

Using the Attr method to set the Class property is a one-time override setting, and the newly set class overrides the original class and uses the above 3 methods to manipulate the class properties without affecting the original other classes.

3. Content/Text operation method

HTML (), text (), Val (): Get without arguments, pass in the value you need to set as a parameter

4. Comprehensive case
<!DOCTYPE HTML><HTML><Head><MetaCharSet= "Utf-8"><title></title>    <style>. tored{width:200px;Height:30px;background:Red;        }. Togreen{width:200px;Height:30px;background:Green;        }#div1{width:250px;Height:30px;background:Red;        }#ddiv1{width:200px;Height:30px;background:Green;        }    </style>    <Scriptsrc= "Js/jquery.js"></Script></Head><Body><DivID= "Div1">Dot I set my id property to Ddiv1</Div><DivID= "Div2"></Div><ButtonID= "BTN1">Setting the Div2style Property</Button><ButtonID= "BTN2">Remove Div2style Property</Button><DivID= "Div3"class= "Tored">Click I switch class</Div><Script>    $(function(){        $("#div1"). Click (function(){            $( This). attr ("txt1","txt1 Effective"); $( This). attr ("ID","Ddiv1"); $( This). Text ("I'm ddiv1 now.");        }); $("#btn1"). Click (function(){            $("#div2"). attr ("style","width:300px;height:30px;background:red;");        }); $("#btn2"). Click (function(){            $("#div2"). Removeattr ("style");        }); $("#div3"). Click (function(){            $( This). Toggleclass ("tored Togreen"); //$ (this). Toggleclass ("tored"). Toggleclass ("Togreen"); Equivalent        }); });</Script></Body></HTML>

Methods for jquery Operation Properties

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.