HTML DOM Input URL placeholder 属性

htmlweb developmentfront end technology更新于 2025/6/16 1:48:17

HTML DOM Input URL 占位符属性设置/返回一个字符串,通常用于提示用户输入文本的外观。

语法

以下是语法 −

  • 返回字符串值
inputURLObject.placeholder
  • 将 placeholder 设置为 stringValue
inputURLObject.placeholder = stringValue

示例

让我们看一个 Input URL placeholder 属性的示例 −

<!DOCTYPE html>
<html>
<head>
<title>Input URL placeholder</title>
<style>
   form {
      width:70%;
      margin: 0 auto;
      text-align: center;
   }
   * {
      padding: 2px;
      margin:5px;
   }
   input[type="button"] {
      border-radius: 10px;
   }
</style>
</head>
<body>
<form>
<fieldset>
<legend>URL-placeholder</legend>
<label for="URLSelect">URL:
<input type="url" id="URLSelect">
</label>
<input type="button" value="Get an Example" onclick="setPlaceholder()">
</fieldset>
</form>
<script>
   var inputURL = document.getElementById("URLSelect");
   function setPlaceholder() {
      inputURL.placeholder = 'https://www.google.com';
   }
</script>
</body>
</html>

输出

这将产生以下输出 −

点击"获取示例"按钮之前 −

点击"获取示例"按钮之后 −


相关文章


有用资源