我试图在加载时使用 Javascript 更改img
src
。但我所得到的似乎并不想工作。有什么想法吗?
(img src的差异为png
和svg
)
HTML
<img class="fusion-logo-1x" src="//new.network-data-cabling.co.uk/wp-content/uploads/2016/08/ACCL_Logo.png" width="400">
<script>
<head>
代码
window.onload = function() {
document.getElementsByClassName("fusion-logo-1x").src = "//new.network-data-cabling.co.uk/wp-content/uploads/2016/08/ACCL_Logo.svg";
};
链接到JSfiddle
答案 0 :(得分:1)
Document.getElementsByClassName()
返回一个类似数组的对象,你需要使用index来迭代并访问它的属性。
document.getElementsByClassName("fusion-logo-1x")[0].src = "//new.network-data-cabling.co.uk/wp-content/uploads/2016/08/ACCL_Logo.svg";
答案 1 :(得分:0)
使用此脚本
window.onload = function() {
document.getElementsByClassName("fusion-logo-1x")[0].src = "//new.network-data-cabling.co.uk/wp-content/uploads/2016/08/ACCL_Logo.svg";
};