代码之家  ›  专栏  ›  技术社区  ›  Syed

如何使用ref获取所有列表项?

  •  0
  • Syed  · 技术社区  · 8 年前

    我知道怎么做父母 ul 只要做 this.$refs.listOfItems 但不知道如何获得所有 li s英寸 联邦制药 列表

    我也知道JS的做法 document.getElementById("listOfItems").getElementsByTagName("li"); 但如上所述,我正在寻找Vue提供的助手。js类 this.$refs

    <template>
      <ul id="listOfItems" ref="listOfItems">
        <li v-for="item in items">{{ item.name }}</li>
      </ul>
    </template>
    
    <script>
      export default {
        mounted: {
          let ul = this.$refs.listOfItems
          let lis =  // HOW TO GET ALL LIs using `this.$refs.listOfItems`
        }
      }
    </script>
    
    1 回复  |  直到 8 年前
        1
  •  2
  •   Decade Moon    8 年前

    我想你是想把 v-for 在 <li> 而不是 <ul> .如果你把 ref 在重复使用的元素上 v-用于 ,则它将是一个项目数组。

    <ul>
      <li v-for="item in items" ref="items">{{ item.name }}</li>
    </ul>
    
    this.$refs.items[0];  // The first <li>