赞
踩
批量获取Dom的方法
:ref="setRef"
<template> <div>获取多个DOM元素</div> <ul> <li v-for="(item, index) in arr" :key="index" :ref="setRef"> {{ item }} </li> </ul> </template> <script> import { ref, nextTick } from 'vue'; export default { setup() { const arr = ref([1, 2, 3]); // 存储dom数组 const myRef = ref([]); const setRef = (el) => { console.log(el,'') myRef.value.push(el); }; nextTick(() => { console.dir(myRef.value[0].innerHTML="你好","多个dom示例"); }); return { arr, setRef }; } }; </script>
二获取单个dom的方法
<template> <div>获取多个DOM元素</div> <ul> <li v-for="(item, index) in arr" :key="index" ref="myRef"> {{ item }} </li> </ul> </template> <script> import { ref, nextTick } from 'vue'; export default { setup() { const arr = ref([1, 2, 3]); // 存储dom数组 const myRef = ref(unll); nextTick(() => { console.dir(myRef.value.innerHTML="你好","单个dom示例"); }); return { arr, myRef }; } }; </script>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。