vue中动态给自定义属性data-xx赋值并读取内容
2023-09-11 14:19:38 时间
1、静态赋值
<img class="imgs-coat" v-for="(item,index) in coatImgs" :key="item.coatImg" :src="item.coatImg" alt="" data-item="123" @click="chooseCoat($event,index)">
获取自定义属性
chooseCoat(e,index){ this.coatIndex = index; console.log(e.target.dataset.item);//123 },
2、动态赋值
动态赋值和静态赋值的区别就是在data-xx前面➕:
1)动态赋值内容为字符串
<img class="imgs-coat" v-for="(item,index) in coatImgs" :key="item.coatImg" :src="item.coatImg" alt="" :data-item="item" @click="chooseCoat($event,index)">
获取属性同上
2)动态赋值内容为对象
<img class="imgs-coat" v-for="(item,index) in coatImgs" :key="item.coatImg" :src="item.coatImg" alt="" :data-item="JSON.stringify(item)" @click="chooseCoat($event,index)">
获取属性
//如果不转换成字符串在转换为对象,只能显示[object,object] chooseCoat(e,index){ this.coatIndex = index; console.log(JSON.parse(e.target.dataset.item)) },
转 : https://blog.csdn.net/wuguidian1114/article/details/103473649
相关文章
- Vue的computed(计算属性)使用实例之TodoList
- vue 新窗口打开页面
- [Vue @Component] Write Vue Functional Components Inline
- [Vue @Component] Define Props on a Vue Class with vue-property-decorator
- Vue.js3: 页面打开前实现图片全部预加载(vue@3.2.33)
- Vue computed属性
- Vue的简单入门
- [Vue @Component] Simplify Vue Components with vue-class-component
- [Vue] Get up and running with vue-router
- vue.js3:解析xml (x2js@3.4.3 / vue@3.2.37)
- vue.js3:使用clipboard.js实现复制到剪贴板(vue@3.2.37 / clipboard@2.0.11)
- vue项目怎么进行seo的优化白屏太久怎么处理?
- 一个典型的Vue应用的App.vue
- vue 向对象中添加未在data中声明的属性
- 在vue里面使用高德地图的详细步骤:怎么设置中心点center?还有坐标点的position值?(文末有代码)
- vue获取DOM元素并设置属性
- vue之全局守卫
- Vue中vue-i18n结合element-ui实现国际化
- 181:vue+openlayers 加载解析geojson文件,给每一个feature(非整体)添加渐变颜色
- 057:vue+openlayers地图图像的滤镜处理(示例代码)
- 说说Vue-Router和Vue组件中的name属性的使用区别
- 基于Java+SpringBoot+Vue+Uniapp(有教程)前后端分离健身预约系统设计与实现
- Vue.js 计算属性
- Vue.js 目录结构
- 【三十天精通Vue 3】第十一天 Vue 3 过渡和动画详解