The example in this article describes how to operate JQuery node element attributes. Share it with everyone for your reference. The specific analysis is as follows:
In JQuery, use the attr() method to get and set element attributes, and the removeAttr() method to delete element attributes.
Getting properties and setting properties
If you want to get the attribute title of the p element, you only need to pass one parameter to the attr() method, which is the attribute name.
var $para = $("p"); //获取<p>节点 var p_txt = $para.attr("title"); //获取<p>元素节点属性title
If you want to set the value of the attribute title of the
element, you can also use the same method. The difference is that you need to pass two parameters, namely the attribute name and the corresponding value.
$("p").attr("title", "your title"); //设置单个的属性值
If you need to set multiple attributes for the same element at once, you can use the following code to achieve it:
$("p") .attr({"title" : "your title", "name": "test"}); //将一个“名/值”形式的对象设置为匹配元素的属性
Many methods in JQuery use the same function to implement getter and setter. For example, the attr() method above can not only set the value of element attributes, but also obtain the value of element attributes. Similar methods include html(), text(), height(), width(), val(), and css().
Delete attribute
In some cases, you need to remove a specific attribute of an element in the document. You can use the removeAttr() method to complete the task.
If you need to delete the title attribute of the p element, you can use the following code to achieve it:
$("p").removeAttr("title"); //删除<p>元素的属性title
You can see it very clearly under Firebug.
I hope this article will be helpful to everyone’s jQuery programming.