zoukankan      html  css  js  c++  java
  • vue中动态给自定义属性data-xx赋值并读取内容

    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

  • 相关阅读:
    [CF1475F] Unusual Matrix
    [JXOI2018] 游戏
    [ZJOI2010] 排列计数
    [CF1474E] What Is It?
    [CF375D] Tree and Queries
    [CF519E] A and B and Lecture Rooms
    [CF321C] Ciel the Commander
    [CF1C] Ancient Berland Circus
    [CF321A] Ciel and Robot
    [CF1450C1] Errich-Tac-Toe (Easy Version)
  • 原文地址:https://www.cnblogs.com/fps2tao/p/13743178.html
Copyright © 2011-2022 走看看