[英]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.