gpt4 book ai didi

javascript - 页面加载时在文本字段中插入当前 url

转载 作者:行者123 更新时间:2023-12-03 05:14:59 25 4
gpt4 key购买 nike

我希望在页面加载时将我的 URL 显示在文本字段中。我尝试了这个,但没有成功:

function getUrl() {
var url = document.URL;
document.getElementById("textfield").value = url;
}
<input type="text" name="textfield" id="textfield" readonly="true" onload="getUrl()" value="">

最佳答案

The onload attribute will not work on an input element 。相反,您可以在窗口上设置一个监听器来监视页面何时“加载”。加载后,调用该函数:

function getUrl() {
var url = document.URL;
document.getElementById("textfield").value = url;
}

window.addEventListener('load', getUrl);
<input type="text" name="textfield" id="textfield" readonly="true" value="">

关于javascript - 页面加载时在文本字段中插入当前 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41645152/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com