JQuery node element attribute operation method, jquery node Operation Method

Source: Internet
Author: User

JQuery node element attribute operation method, jquery node Operation Method

This document describes how to operate element attributes of a JQuery node. 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"); // obtain the <p> node var p_txt = $ para. attr ("title"); // obtain the <p> element node attribute title

If you want to set the attribute title value of the <p> element, you can also use the same method. The difference is that two parameters must 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 the attribute title of the <p> element

You can see it clearly in Firebug.

I hope this article will help you with jQuery programming.

Related Article

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.