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

2025-04-30 19:19:27
推荐回答(1个)
回答1:




    
    
    
        *{margin: 0; padding: 0;}
        html, body{width: 100%; height: 100%; position: relative;}
        img{width: 100px; height: 100px;}
    




    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);
        }
    }


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