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

Setting Input field to contain window.location.href

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

我建议的方法:

<script>
    var holder = window.location.href;
</script>

<input id="moo" value= holder>

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;
  });
});

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

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

使用JavaScript,我们可以轻松地select这个元素调用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">