#clearBtn {display: inline-block;}然后,我们需要使用 JavaScript 来监听清空按钮的点击事件,以及清空输入框的内容。注意,此处我们在 HTML 中为清空按钮添加了 id 属性为 clearBtn,在 JavaScript 中可以使用 document.getElementById("clearBtn") 获取该按钮的节点对象:document.getElementById("clearBtn").onclick = function() {document.getElementById("inputBox").value = "";};以上代码的作用是:在清空按钮被点击时,获取 id 为 inputBox 的输入框节点对象,然后将其 value 属性设为空字符串,即清空输入框内容。最后,我们还可以为清空按钮添加一些基础的样式美化:#clearBtn {display: inline-block;border: none;outline: none;font-size: 14px;cursor: pointer;color: #fff;background-color: #00bcd4;padding: 6px 10px;}#clearBtn:hover {background-color: #0097a7;}完整代码如下所示:#clearBtn {display: inline-block;border: none;outline: none;font-size: 14px;cursor: pointer;color: #fff;background-color: #00bcd4;padding: 6px 10px;}#clearBtn:hover {background-color: #0097a7;}document.getElementById("clearBtn").onclick = function() {document.getElementById("inputBox").value = "";};通过以上代码,我们成功实现了一个简单的“清空”按钮功能。希望本文对大家学习 CSS 有所帮助。上一篇:css按钮样式点击_禁用
下一篇:css按钮离上边框









