JQuery learning 3: Operating element attributes and features _ jquery

Source: Internet
Author: User
Today, I will summarize some of the jQuery operation element attributes. For more information about jquery, see the following example:

The Code is as follows:


The http://www.jb51.net now gets the property id of the tag. There are the following methods:

The Code is as follows:


JQuery ("# easy"). click (function (){
Alert (document. getElementById ("easy"). id); // 1
Alert (this. id); // 2
Alert (jQuery (this). attr ("id"); // 3
});

Method 1 uses the original javascript method. method 2 uses this, which is equivalent to a pointer. a dom object is returned. In this example, the tag object is returned. Therefore, this. id can be obtained directly. Method 3 converts a dom object to a jQuery object, and obtains the ID of tag a using the method attr () encapsulated by jQuery.

It can be seen that it is very convenient to use javascript with jQuery. The following describes the jQuery operation element attributes.

◦ Attr (name) gets the attribute value of an element.
◦ Attr (properties) sets the element attribute in the form of name/value
◦ Attr (key, value) sets the attribute value for the element
◦ RemoveAttr (name) removes the attribute value of an element.

The following example describes the specific usage of each method.

The Code is as follows:



Foot home
Baidu


The Code is as follows:


JQuery ("# test a"). click (function (){
// Obtain the ID
JQuery (this). attr ("id"); // same as this. id

// Set src to the specified image for the img label; title to Baidu.
Var v = {src: "http://www.baidu.com/img/bdlogo.gif", title: "Baidu "};
JQuery ("# show"). attr (v );

// Set img title to baidu. The difference between the two sides is that only one attribute can be set at a time.
JQuery ("# show"). attr ("title", "baidu ");

// Remove the img title attribute
JQuery ("# show"). removeAttr ("title ");
});

You may have discovered that the attr () method in jQuery can obtain the attribute values of an element and set the attribute values of an element. Yes, there are still many similar methods in jQuery. Now I will summarize them and it will be easier to use them later.

Methods:

Using html () to obtain or set the html content of an element node
◦ Text () gets or sets the text content of an element node
◦ Height () gets or sets the element height
Trim width () to get or set the element width
Using val () to get or set the value of the input box

Take html () as an example. The rest are similar:

The Code is as follows:


Baidu


The Code is as follows:


// Obtain html and the result is baidu
JQuery ("# showhtml" ).html ();
// Set html and the result is I love baidu
JQuery ("# showhtml" ).html ("I love baidu ");

These are some basic methods for jQuery to operate on element attributes. After this conclusion, we believe that you will be more proficient in using jQuery.

The following is a supplement from other netizens:

The methods provided in jQuery are listed below:

Operation element attribute: each (iterator) traverses all elements in the package and calls the passed iterator functions for each element. The iterator parameter is a function that is called once for each element in the matching set. The parameter passed to the function is set to the subscript of the current element in the package set (starting from 0), and the current element can be accessed through the this attribute of the function.

The Code is as follows:


$ ('Img '). each (function (n ){
This. alt = 'this is image ['+ n +'] with an id of '+ This. id;
})

Get feature value: attr (name) gets the value of the specified feature assigned to the first element in the package. The parameter name is the name of the feature. The value of this feature is obtained. If this feature is not available, the undefined value is returned.

$ ("# MyImage"). attr ("custom") returns some value.

Set feature value: attr (name, value) sets the passed value for the name feature of all elements in the package. Name indicates the name of the feature to be set. value indicates the value of the feature.

The Code is as follows:


$ ('*'). Attr ('title', function (index ){
Return 'I am element ''+ index +' and my name is '+ (this. id? This. id: 'unset ');
});

This function sets the title attribute of all elements on the page as a string. A string consisting of the subscript of the element in the DOM and the id feature value of each specific element.

Attr () also allows you to set multiple features at a time to a quick and easy way for all elements in the package. Attr (attributes ).

The Code is as follows:


$ ('Input'). attr (
{Value: '', title: 'Please enter a value '}
);

This function sets allThe value of the element is set to an empty string, and the title is set to the string Please enter a value.

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.