小程序图片404错误,使用默认图片替换

mac2022-06-30  93

image的属性

 

 

微信小程序图片加载失败显示默认图片,通常情况下:

wxml

<image class="userinfo-avatar" src="{{avatar}}" binderror="errorFunction"></image>

js

errorFunction: function(){ this.setData({ avatar: '/image/head.png' }) }

  

若使用了 for 循环,代码如下

wxml

<view wx:for="{{slider}}" wx:key="{{key}}" wx:for-index='index'> <image src='{{item.picUrl}}' binderror="imageError" data-index='{{index}}'></image> </view>

  

js

imageError: function(e) { var index = e.currentTarget.dataset.index var img = 'slider[' + index + '].picUrl' this.setData({ [img]: '../../images/default.png' }) },

  

 

转载于:https://www.cnblogs.com/YAN-HUA/p/9776579.html

相关资源:JAVA上百实例源码以及开源项目
最新回复(0)