$pieces[12]
中的变量是我的服务器上图像的URL,有时当页面加载时,图像会损坏。我的脚本假设找到损坏的图像错误(如果有),然后再次将其替换为$pieces[12]
。如果这听起来没错。但是我不知道如何使用我的代码来做到这一点。
<div class="right_ad" id="right_ad">
<script>
<img src="<?php echo $pieces[12]; ?>"
onError="this.onerror=null;this.src='<?php echo $pieces[12]; ?>';"
style="width: 100%;max-height: 100%"/>
</script>
</div>
您应该urlencode
这些值。我的提示是您的文件名中有特殊值。
function onError_run_this_func(){
//get your new image url with ajax function
//or if you already have it in $pieces[12] then use it.
$.('.image-class).attr('src', '<?php echo $pieces[12]; ?>');
}
<div class="right_ad" id="right_ad"><script><img class="image-class" src="`<?php echo $pieces[12]; ?>" onError="onError_run_this_func()" style="width: 100%;max-height: 100%"/></script> </div>`