【发布时间】:2018-07-06 15:56:38
【问题描述】:
我有这个脚本,它为 img 标签添加一个 ID,然后是一个函数,它用不同颜色的版本更改图像 src,如下所示:
<script type="text/javascript">
jQuery(document).ready(function( $ ) {
$('.custom-logo-link img').each(function(i) {
$(this).attr('id', 'img-logo' + i);
});
});
function setImg(){
var numRand = Math.floor((Math.random() * 4) + 1);
document.getElementById("img-logo0").src = "/wp-content/uploads/2018/04/Logo"+numRand+".jpg";}
setImg();
window.setInterval("setImg()",1000);
</script>
我的问题是我不断收到 Uncaught TypeError: Cannot set property 'src' of null 但我已经设置了它应该查看的 ID。
【问题讨论】:
-
您能否提供一个有效的 js/html 代码示例?