我在互联网上找到了以下脚本。
如何更改代码,以便在点击Open
按钮时,它会转到lnk.href
("https://www.google.si/maps/place/ljubljana" + userInput;
)中的地址
<body>
<script type="text/javascript">
function changeText2(){
var userInput = document.getElementById('userInput').value;
var lnk = document.getElementById('lnk');
lnk.href = "https://www.google.si/maps/place/ljubljana" + userInput;
lnk.innerHTML = lnk.href;
}
</script>
Type the location and click Open! <a href="" id=lnk>link</a> <br>
<input type='text' id='userInput' value='' />
<input type='submit' onclick='changeText2()' value='Open'/>
答案 0 :(得分:0)
您希望在功能结束时添加window.location = lnk.href;
:
<script>
function changeText2() {
var userInput = document.getElementById('userInput').value;
var lnk = document.getElementById('lnk');
lnk.href = "https://www.google.si/maps/place/ljubljana" + userInput;
lnk.innerHTML = lnk.href;
window.location = lnk.href;
}
</script>
Type the location and click Open! <a href="" id=lnk>link</a> <br>
<input type='text' id='userInput' value='' />
<input type='submit' onclick='changeText2()' value='Open'/>
您也可以取消链接:
<script>
function openUrl() {
var userInput = document.getElementById('userInput').value;
window.location = "https://www.google.si/maps/place/ljubljana" + userInput;
}
</script>
Type the location and click Open!<br>
<input type='text' id='userInput' value='' />
<input type='submit' onclick='openUrl()' value='Open' />