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"); // GetNode 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"); // deleteAttribute title of an element
You can see it clearly in Firebug.
I hope this article will help you with jQuery programming.