v-for 怎么实现怎么实现对某个指定元素的文本节点的获取呢?

通过v-for生成列表,每行中的tr都有复制按钮,点击复制实现对tr中的某个td文本节点copy,现在是不知道怎么获取到item.code文本节点,这个具体代码如下:

<tr v-for="(item,index) in userListLimit" :key="index">

            <td>{{item.inserted_at}}</td>
            <td ref="itemCode">{{item.code}}</td>
            <td>{{item.inviter}}</td>
            <td>{{item.invitee}}</td>
            <td>{{item.when_long}}</td>
            <td :title="item.note">{{item.note}}</td>
            <td>
                <a v-if= "item.invitee==''?true:false" @click="myCopy($event)">复制</a>
            </td>

</tr>
// 点击复制到剪贴板

  myCopy(event) {
   // event.target.parentNode.previousSibling.select()
    document.execCommand('Copy')
  },
阅读 1.8k
2 个回答

我大概会这样子做

<td :class="'cls_'+item.code">....</td>
<td><a @click="myCopy(item.code)">复制</td>
myCopy(code){
//document.getElementByClassName('cls_'+code)
}

复制文本的话可参考:https://www.cnblogs.com/wisew...

直接把code作为参数,myCopy里面修改
myCopy($event),event有用到吗,没用的话myCopy(item.code),有用的话myCopy($event,item.code);

然后在tr上绑定ref,这样通过vue的ref就能找到节点啦。
<tr v-for="(item,index) in userListLimit" :key="index" :ref='item.code'>

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题