Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to delete a specified attribute in an element in javascript

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Javascript how to delete the specified attributes in the element, I believe that many inexperienced people do not know what to do, so this article summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

In JavaScript, you can use the removeAttribute () method to delete the specified attribute of the element, with the syntax "removeAttribute (name)" and the parameter name indicating the attribute name of the element to be deleted.

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

In JavaScript, you can delete a specified attribute using the removeAttribute () method of the element. The usage is as follows:

RemoveAttribute (name)

The parameter name represents the attribute name of the element.

Example 1

The following example shows how to set the border of a table dynamically.

_ window.onload = function () {/ / event handler function var table = document.getElementByTagName ("table") [0] when the binding page is loaded; / / get the reference to the outer box of the table var del = document.getElementById ("del"); var reset = document.getElementById ("reset"); del.onclick = function () {table.removeAttribute ("border") } reset.onclick = function () {table.setAttribute ("border", "2");} data table deletion and recovery

In the above example, two buttons are designed and bound to different event handlers. Click the "delete" button to call the removeAttribute () method of the table to clear the border of the table, and click the "restore" button to call the setAttribute () method of the table to reset the thickness of the cousin.

Example 2

The following example shows how to customize the delete class function and call it to delete the specified class name.

Function hasClass (element, className) {/ / Class name detection function var reg = new RegExp ('(\\ s | ^)'+ className +'(\\ s | $)'); return reg.test (element, className); / / use regular detection to see if there is the same style} function deleteClass (element, className) {if (hasClass (element, className)) {element.className.replace (reg,'') / / capture the style to be deleted and replace it with a blank string} box var red = document.getElementById ("red"); deleteClass (red, 'blue')

The above code uses a regular expression to detect whether the specified class name is included in the className attribute value string, and if so, replaces the matching substring with an empty string to achieve the purpose of deleting the class name.

After reading the above, have you mastered the method of how to delete the specified attribute in the element in javascript? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report