How to modify the attributes of element nodes in javascript

青灯夜游
Release: 2022-02-16 11:25:16
Original
3636 people have browsed it

Modification method: 1. Use the setAttribute() method to modify the value of the node attribute, the syntax "node.setAttribute(attribute name, value)"; 2. Use the removeAttribute() method to delete the specified attribute, the syntax "node .removeAttribute(attribute name)".

How to modify the attributes of element nodes in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

Javascript modifies the attributes of the element node

setAttribute() method to set the attribute value

Use The element's setAttribute() method can set the element's attribute value. The usage is as follows:

setAttribute(name, value)
Copy after login

The parameters name and value represent the attribute name and attribute value respectively. Property names and property values must be passed as strings. If the specified attribute exists in the element, its value will be refreshed; if it does not exist, the setAttribute() method will create the attribute for the element and assign a value.

红盒子
蓝盒子
Copy after login

removeAttribute() method removes attributes

Use the element's removeAttribute() method to remove the specified attribute. The usage is as follows:

removeAttribute(name)
Copy after login

Parameter name represents the attribute name of the element.

Example:

 
数据表格
Copy after login

In the above example, two buttons are designed and bound to different event handling functions. Click the "Delete" button to call the table's removeAttribute() method to clear the table border, and click the "Restore" button to call the table's setAttribute() method to reset the thickness.

[Related recommendations:javascript learning tutorial]

The above is the detailed content of How to modify the attributes of element nodes in javascript. For more information, please follow other related articles on the PHP Chinese website!

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
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!