【问题标题】:How to automatics go on url address without clicking on it如何在不点击的情况下自动进入 url 地址
【发布时间】:2014-08-07 23:43:40
【问题描述】:

我在 Internet 上找到了以下脚本。

如何更改代码,以便在单击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'/>

【问题讨论】:

    标签: javascript html url


    【解决方案1】:

    您想在函数末尾添加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' />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-26
      • 2021-01-17
      • 1970-01-01
      • 2021-12-06
      • 1970-01-01
      • 2021-11-23
      • 2017-12-15
      相关资源
      最近更新 更多