【问题标题】:Setting Input field to contain window.location.href将输入字段设置为包含 window.location.href
【发布时间】:2021-06-04 22:33:06
【问题描述】:

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

我建议的方法:

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

<input id="moo" value= holder>

【问题讨论】:

  • 我不明白这个问题。
  • @nicael- 我澄清了这个问题
  • 但是您的代码似乎与问题无关?..
  • 这些都没有意义。目标不明确,似乎与显示的代码没有任何关系。请准确说明您想要完成的任务和预期的行为
  • 一个 div 有文本或 html 没有值

标签: javascript jquery window.location


【解决方案1】:

要将文本输入字段的值设置为当前页面的位置,首先,我们必须正确设置 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;
&lt;input type="text" id="moo"&gt;

【讨论】:

  • 谢谢!很好的答案!
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2022-01-24
    • 2019-05-07
    • 1970-01-01
    • 1970-01-01
    • 2020-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-18
    相关资源
    最近更新 更多