Javascript 是否自动将window.location.hash插入html输入值?
以下是我基本上想要做的:Javascript 是否自动将window.location.hash插入html输入值?,javascript,html,Javascript,Html,以下是我基本上想要做的: <input type="text" class="form-control" value="window.location.hash"> 将窗口.location.hash插入输入值的正确方法是什么 注意:我找到了几种方法,可以在人们需要单击按钮时执行此操作,但没有任何方法解释如何在页面加载时自动执行此操作。您需要在页面加载后分配此操作,或者至少分配元素 <input type=&qu
<input type="text" class="form-control" value="window.location.hash">
将窗口.location.hash
插入输入值的正确方法是什么
注意:我找到了几种方法,可以在人们需要单击按钮时执行此操作,但没有任何方法解释如何在页面加载时自动执行此操作。您需要在页面加载后分配此操作,或者至少分配元素
<input type="text" class="form-control" id="hash" value="">
<script>
window.onload=function() {
document.querySelector("#hash").value = window.location.hash
}
</script>
window.onload=function(){
document.querySelector(“#hash”).value=window.location.hash
}
只需获取元素并使用JavaScript更改其值即可。在这段代码中,我将重定向到另一个散列,只是出于示例目的
const-input=document.querySelector(“input.form-control”);
//例如重定向到不同的散列
window.location.hash=“abcdef”;
input.value=window.location.hash代码>
您需要JS脚本:
document.addEventListener('DOMContentLoaded',()=>{
常量输入=document.getElementById('location');
input.value=window.location.host;//将.host更改为.hash
})
这是否回答了您的问题?