[英]set value of html input or textarea
这是完美的工作。 我想获取当前URL并将其设置在textarea或输入标签内。 实际上,该值设置为p
标签。
function urlf() { document.getElementById("root").innerHTML = "The full URL of this page is: < br > " + window.location.href; }
<h2>JavaScript</h2> <h3>The window.location.href</h3> <button id="my btn" type="button" onclick="urlf()"> get </button> <p id="root"> </p>
我怎样才能做到这一点?
创建一个input
标签并设置其value
function urlf() { document.getElementById("root").value = "The full URL of this page is:" + window.location.href; }
<h2>JavaScript</h2> <h3>The window.location.href</h3> <button id="my btn" type="button" onclick="urlf()"> get </button> <input type="text" style="width:100%;" id="root">
您可以使用id root
设置元素的innerHTML
。 根据您的代码,这是:
<p id="root"> </p>
因此,如果要将内容加载到输入字段中,则需要更改以下内容:
document.getElementById("root").value = "The full URL of this page is:" + window.location.href;
<input id="root" type="text" />
问候
您只需要输入当前的URL? 如下所示:
function urlf() { document.getElementById("root").value = "The full URL of this page is:" + window.location.href; }
<div> <h2>JavaScript</h2> <h3>The window.location.href</h3> <button id="my btn" type="button" onclick="urlf()"> get </button> <input style="width:500px;" type="text" id="root"/> </div>
<input id="root" type="text">
function urlf() {
document.getElementById("root").value = window.location.href;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.