Home > Web Front-end > JS Tutorial > body text

JQuery node element attribute operation method_jquery

WBOY
Release: 2016-05-16 15:55:50
Original
1172 people have browsed it

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
Copy after login

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");
//设置单个的属性值

Copy after login

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"});   
//将一个“名/值”形式的对象设置为匹配元素的属性

Copy after login

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
Copy after login

You can see it very clearly under Firebug.

I hope this article will be helpful to everyone’s jQuery programming.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!