关于html data-*详细介绍:
https://www.w3school.com.cn/tags/att_global_data.asp
在项目开发中,我们可以使用data-*来嵌入自定义的数据,比如在实际场景中,我们需要把后端接口获取到的对象传送到js中进行下一步处理,例如:
:data-后面为我们传递的参数名,在js方法中我们可以使用该参数名进行调用,当我们传递的值为对象时,该方法不可取,因为data-*只支持字符串传递,故我们在该场景中从for循环中取到的对象值不可取,但是我们可以把对象先转换成字符串类型,转递过去之后再转换成对象,如此一来遍解决这一问题。
<div v-for="item in list" class="content" :data-item=JSON.stringify(item)>
<div class="content-top">
{{item.name}}
</div>
<img class="next" src="../img/next.png">
</div>
再触屏点击方法里面我们对其进行转换成对象处理:
$(function () {
//mui触屏点击
$("#bg").on('tap', '.content', function (event) {
console.log(event);
console.log(JSON.parse(event.currentTarget.dataset.item));
let item=JSON.parse(event.currentTarget.dataset.item);
});
})