jquery node Element attribute operation method _jquery

Source: Internet
Author: User

This example describes the jquery node element attribute operation method. Share to everyone for your reference. The specific analysis is as follows:

In jquery, the attr () method is used to get and set the element properties, Removeattr () method to delete the element attributes.

Getting properties and setting properties

If you want to get the property title of the P element, you only need to pass a parameter to the attr () method, which is the property name.

var $para = $ ("P"); Get <p> node  
var p_txt = $para. attr ("title");//Get <p> element node property title

If you want to set the value of the property title of the <p> element, you can also use the same method, and the difference is that you need to pass two arguments, namely the property name and the corresponding value.

$ ("P"). attr ("title", "Your title");
To set individual property values

If you need to set multiple properties for the same element at once, you can use the following code to implement:

$ ("P"). attr ({"title": "Your title", "Name": "Test"});   
Sets an object of type "name/value" to the property of the matching element

Many of the methods in jquery are the same function implementation fetch (getter) and set (setter), such as the attr () method above, which can either set the value of the element property or get the value of the element property. Similarly, there are methods such as HTML (), text (), height (), Width (), Val (), and CSS ().

Delete attribute

In some cases, you need to remove specific attributes for an element in a document, and you can use the Removeattr () method to complete the task.

If you need to remove the title attribute of the P element, you can use the following code to implement:

$ ("P"). Removeattr ("title");
Delete the attributes of the <p> element title

You can see it very clearly under the Firebug.

I hope this article will help you with your 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.