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

当前元素数据属性中的访问循环索引-VueJS

  •  2
  • Towkir  · 技术社区  · 7 年前

    v-for 指令。

    HTML内容是:

    <div id="app">
        <div v-for="count in length" data-my-attribute="here" class="target">{{count}}</div>
    </div>
    

    以及JS代码:

    var app = new Vue({
        el : '#app',
        data: {
            length: 9,
        }
    });
    

    我知道我可以访问 div target . 就像它处理 {{ count }}

    但是有没有可能进入 count data-my-attribute ?

    (我的意思是代替“这里”)

    1 回复  |  直到 5 年前
        1
  •  3
  •   Boussadjra Brahim    7 年前

    您可以使用绑定访问该变量,如下所示:

    <div id="app">
      <div v-for="count in length" :data-my-attribute="count" class="target">{{count}} </div>
    </div>
    

    就像你想定义一个动态的 id

    <div id="app">
      <div v-for="count in length" :id="'divNum'+count" class="target">{{count}} </div>
    </div>
    
    推荐文章