HTML DOM Input Text placeholder 属性
htmlweb developmentfront end technology更新于 2025/9/27 16:44:17
HTML DOM Input Text placeholder 属性用于设置或返回输入文本字段的占位符属性值。占位符属性用于在用户输入任何内容之前,在输入字段内显示一段文本,向网页用户提示输入元素。占位符文本默认为灰色,并且与 value 属性不同,不会提交到表单。
语法
以下是语法 −
设置占位符属性 −
textObject.placeholder = text
此处,text 表示占位符文本,用于向用户提示有关文本字段的内容。
示例
让我们来看一个 Input Text placeholder 属性的示例 −
<!DOCTYPE html>
<html>
<body>
<h1>Input Text placeholder property</h1>
USERNAME: <input type="text" id="USR" placeholder="....">
<p>Change the placeholder text of the above field by clicking the below button</p>
<button onclick="changeHolder()">CHANGE</button>
<script>
function changeHolder() {
document.getElementById("USR").placeholder = "Enter your username here..";
}
</script>
</body>
</html>
输出
这将产生以下输出 −

占位符修改成功 −

相关文章
有用资源
html 参考教程 - 该教程包含有关 html 的更多信息:https://www.cainiaomax.com/html/

