javascript - 使用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
,但我不知道该怎么做。这怎么可能?
解决方案
将此添加到脚本中代码的末尾
document.getElementById("instagram").href = instagram
推荐阅读
- python - 评估字符串的内容并匹配元组?
- python - 如何将文件从 docker 容器复制到 Jenkins
- jestjs - 无法使用 jest 测试从 node_modules 内部导入 SCSS 的 Svelte 组件
- javascript - WebGL Stencils,如何使用 2D 精灵的透明度作为蒙版?
- javascript - 拖动文件时更改元素的 CSS 属性
- mysql - 如何将两列中的不同值计算为一个数字(跟进)
- java - 如何安全地将 int 向下转换为 short?
- json - 如何使用 swift combine 读取嵌套的 json 数据
- php - 每个帖子的 RSS 和 RSS 页面
- r - R工具可视化金融时间序列的汇总统计