欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

微信小程序 image組件binderror使用例子與js中的onerror區(qū)別

 更新時(shí)間:2017年02月15日 10:30:12   作者:無影飛絮劍  
這篇文章主要介紹了微信小程序 image組件binderror使用例子與js中的onerror區(qū)別的相關(guān)資料,需要的朋友可以參考下

微信小程序image組件binderror使用例子(對(duì)應(yīng)html、js中的onerror)

    官方文檔

 binderror HandleEvent 當(dāng)錯(cuò)誤發(fā)生時(shí),發(fā)布到 AppService 的事件名,事件對(duì)象event.detail = {errMsg: 'something wrong'}

在微信小程序開發(fā)中,我們使用列表包含圖片,如果這個(gè)圖片鏈接404錯(cuò)誤,那么我們應(yīng)該給它提供一個(gè)默認(rèn)的友好URL地址。html和js中使用onerror事件就可以了

<img src="image.gif" onerror="this.src='https:w.chesudi.com/Public/web/img/onerrorcar.png'" />

微信小程序image組件沒提供onerror事件,提供了一個(gè)binderror事件來代替。

如果圖片鏈接404,就會(huì)觸發(fā)這個(gè)binderror事件,我們?cè)诮壎ǖ氖录镄薷膶?duì)應(yīng)的數(shù)據(jù)源就好了,如下

.wxml文件

<image class="carlist_img" src="{{item.img}}" binderror="binderrorimg" data-errorimg="{{index}}"></image>

.js文件 

 binderrorimg:function(e){ 
  var errorImgIndex= e.target.dataset.errorimg //獲取循環(huán)的下標(biāo)
  var imgObject="carlistData["+errorImgIndex+"].img" //carlistData為數(shù)據(jù)源,對(duì)象數(shù)組
  var errorImg={}
  errorImg[imgObject]="https://w.chesudi.com/Public/web/img/onerrorcar.png" //我們構(gòu)建一個(gè)對(duì)象
  this.setData(errorImg) //修改數(shù)據(jù)源對(duì)應(yīng)的數(shù)據(jù)
 }

 易錯(cuò)點(diǎn):this.setData({"carlistData["+errorImgIndex+"].img":對(duì)象})類似這樣的就不正確了

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

相關(guān)文章

最新評(píng)論