removeAttribute() 方法用于从元素中移除指定的属性。在表单处理中,removeAttribute() 方法常用于清除表单元素中的某些属性,例如清除输入框的值或禁用属性。
以下是一个示例,演示如何使用 removeAttribute() 方法清除表单元素中的值:
<!DOCTYPE html><html><head><title>Remove Attribute Example</title></head><body><form id="myForm"> <input type="text" id="myInput" value="Hello World"> <button type="button" onclick="clearInput()">Clear Input</button></form><script>function clearInput() { var input = document.getElementById("myInput"); input.removeAttribute("value");}</script></body></html>在上面的示例中,当用户点击按钮时,clearInput() 函数会获取输入框元素并使用 removeAttribute() 方法清除输入框的值。这样就实现了清除表单元素中指定属性的功能。


