首页 > 解决方案 > 使用javascript变量html将链接放入href

问题描述

我有这个:

<script>
    function myFunction() {
    
    //put the url of the girl into the quotes of imgUrl
    var imgUrl = "https://www.linkpicture.com/q/IMG_4902."
    
    //put the name of the model into the quotes of modelName
    var modelName = "modelName"
    
    //put the link to their instagram into the quotes of instagram
    var instagram = "https://www.instagram.com/gianluca/"
    
    
    document.getElementById("myText").innerHTML = modelName;
    document.getElementById("myImg").src = imgUrl;
    document.getElementById("instagram").innerHTML = instagram;
    }
    </script>
<body onload="myFunction()">
            <img src="" id="myImg" alt="">
            <h2 id="myText"></h2>
            <ul>
                <li><a href="" id="instagram"><i class="fab fa-instagram"></i></a></li>

我正在尝试使用变量instagram,并将其放入 中href,但我不知道该怎么做。这怎么可能?

标签: javascripthtml

解决方案


将此添加到脚本中代码的末尾

document.getElementById("instagram").href = instagram

推荐阅读