2014-06-17 65 views
-4

我在Internet上找到了以下腳本。如何在沒有點擊的情況下自動轉到url地址

如何更改代碼,以便在單擊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

你想在你的函數的末尾添加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' /> 
相關問題