JQuery node element attribute operation method _ jquery

Source: Internet
Author: User
This article mainly introduces the JQuery node element attribute operation method. The example analyzes the usage skills of the attr and removeAttr methods for operating the node element attributes, for more information about how to operate element attributes of JQuery nodes, see the following example. Share it with you for your reference. The specific analysis is as follows:

In JQuery, the attr () method is used to obtain and set element attributes. the removeAttr () method is used to delete element attributes.

Get and set attributes

To obtain the attribute title of the p element, you only need to pass a parameter to the attr () method, that is, the attribute name.

Var $ para = $ ("p"); // Get

Node var p_txt = $ para. attr ("title"); // Get

Element node attribute title

If you want to set

The value of the attribute title of an element can also be used in the same method. The difference is that two parameters need to be passed, namely, the attribute name and the corresponding value.

$ ("P"). attr ("title", "your title"); // you can specify a property value.

To set multiple attributes for the same element at a time, use the following code:

$ ("P "). attr ({"title": "your title", "name": "test"}); // sets an object in the form of "name/value" as an attribute of a matching element.

Many methods in JQuery are implemented by the same function (getter) and setter. for example, the above attr () method can set the value of Element attributes, you can also obtain the value of the element attribute. Similar methods include html (), text (), height (), width (), val (), and css.

Delete attributes

In some cases, you need to delete the specific attributes of an element in the document. you can use the removeAttr () method to complete this task.

To delete the title attribute of the p element, use the following code:

$ ("P"). removeAttr ("title"); // delete

Attribute title of an element

You can see it clearly in Firebug.

I hope this article will help you with jQuery programming.

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.