将输入字段设置为包含 window.location.href

Setting Input field to contain window.location.href

本文关键字:window location href 包含 输入 字段 设置      更新时间:2023-09-26

如何使用Javascript获取输入字段具有当前网页URL的值?

我建议的方法:

<script>
    var holder = window.location.href;
</script>
<input id="moo" value= holder>

要将文本输入字段的值设置为当前页面的位置,首先,我们必须正确设置 HTML:

<input type="text" id="moo">

使用 JavaScript,我们可以通过调用 document.getElementById 来轻松选择此元素,将文本字段的id作为参数传入:

var input = document.getElementById("moo"); // "moo" is the 'id' of the text field

现在,我们可以将文本字段的value分配给location.href

input.value = location.href;

现在,我们的输入字段将包含当前网页的位置。

var input = document.getElementById("moo"); // "moo" is the 'id' of the text field
input.value = location.href;
<input type="text" id="moo">

HTML

<input type="text" id="url" placeholder="type the url to navigate to" />
<button id="search">
  search
</button>

.JS

$(function() {
    $("#search").click(function() {
    var url = $("#url").val();
    window.location.href = url;
  });
});