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.