怎么在html的中调用js变量存储的图片路径

 我来答
jiaozhuangfei
2016-12-04 · TA获得超过698个赞
知道小有建树答主
回答量:316
采纳率:100%
帮助的人:264万
展开全部
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style type="text/css">
        *{margin: 0; padding: 0;}
        html, body{width: 100%; height: 100%; position: relative;}
        img{width: 100px; height: 100px;}
    </style>
</head>
<body>
<img src="" alt="">
<script type="text/javascript">
    var box = document.getElementsByTagName('body')[0],
        img = document.getElementsByTagName('img')[0],
        urlValue = 'https://gss0.baidu.com/7Po3dSag_xI4khGko9WTAnF6hhy/image/pic/item/3bf33a87e950352a5936aa0a5543fbf2b2118b59.jpg';
    box.onclick = function () {
        if(img.getAttribute('src')){
            img.setAttribute('src', '');
        }else {
            img.setAttribute('src', urlValue);
        }
    }
</script>
</body>
</html>

html是不能调用的,它其实还是靠JS来调用,附的代码是点击页面中任何一个地方就会给img赋值src地址,这个是我在baidu上找的一个图片地址,再次点击就去掉img 的src路径。有地址就会显示图片,没有就不会显示了。简单来说,就是给img 的src赋值而已。

推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式