15
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Vue.jsでカスタムデータ属性を使う

Posted at

リストの一つ一つの要素にカスタムデータ属性を指定する。
以下のようにv-bind:を使う(以下は省略表記の:を使っている。)

<ul>
    <li v-for="(value, key) in lists" :data-order-id="value.id">
        {{ key }}:{{ value }}
    </li>
</ul>

このデータをJavascript側で使う

mounted: function () {
    let li = $('ul > li');

    // 一つ一つのli要素に対して処理
    li.each(function () {
        // 通常と同じように.dataでアクセス出来る
        console.log($(this).data('orderId')) // value.idの値が順番にconsoleに表示される
    });
}

:data-order-idではなくdata-order-idだとエラーになりました。

15
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?