HTML DOM Input URL 对象
JavaScript基础 2022-05-13 16:20:51小码哥的IT人生shichen
HTML DOM Input URL 对象
Input URL 对象
Input URL 对象是 HTML5 中的新对象。
Input URL 对象表示 HTML <input type="url">
元素。
注释:Internet Explorer 9(以及更早的版本)或 Safari 不支持 <input type="search">
元素。
访问 Input URL 对象
您可以通过使用 getElementById()
来访问 <input type="url">
元素:
var x = document.getElementById("myUrl");
完整实例:
<!DOCTYPE html>
<html>
<body>
<h3>演示如何访问 URL 字段</h3>
首页:<input type="url" id="myURL" value="http://www.google.com">
<p>点击按钮来获得 URL 字段的 URL。</p>
<button onclick="myFunction()">试一下</button>
<p id="demo"></p>
<script>
function myFunction() {
var x = document.getElementById("myURL").value;
document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
创建 Input URL 对象
您可以通过使用 document.createElement()
方法来创建 <input type="url">
元素:
var x = document.createElement("INPUT");
x.setAttribute("type", "url");
完整实例:
<!DOCTYPE html>
<html>
<body>
<h3>演示如何创建 URL 字段</h3>
<p>点击按钮来创建 URL 字段。</p>
<button onclick="myFunction()">试一下</button>
<script>
function myFunction() {
var x = document.createElement("INPUT");
x.setAttribute("type", "url");
x.setAttribute("value", "http://www.google.com");
document.body.appendChild(x);
}
</script>
</body>
</html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
Input URL 对象属性
属性 | 描述 |
---|---|
autocomplete | 设置或返回 Search 字段的 autocomplete 属性值。 |
autofocus | 设置或返回 Search 字段在页面加载后是否应自动获取焦点。 |
defaultValue | 设置或返回 Search 字段的默认值。 |
disabled | 设置或返回 Search 字段是否被禁用。 |
form | 返回对包含 Search 字段的表单的引用。 |
list | 返回对包含 Search 字段的 datalist 的引用。 |
maxLength | 设置或返回 search 字段的 maxLength 属性值。 |
multiple | 设置或返回是否允许用户在 URL 字段中输入不止一条 url 地址。 |
name | 设置或返回 Search 字段的 name 属性值。 |
pattern | 设置或返回 Search 字段的 pattern 属性值。 |
placeholder | 设置或返回 Search 字段的 placeholder 属性值。 |
readOnly | 设置或返回 Search 字段是否是只读的。 |
required | 设置或返回在提交表单之前是否必须填写 Search 字段。 |
size | 设置或返回 Search 字段的 size 属性值。 |
step | 设置或返回 Search 字段的 step 属性值。 |
type | 返回 Search 字段的表单元素类型。 |
value | 设置或返回 Search 字段的 value 属性值。 |