从事件中获取数据属性

Grab data attribute from an event

我有一个 jQuery 事件,它是 backbone class 的一部分,我正试图从调用的 'data-foreground'[=12= 中获取数据属性]

我的函数如下所示..

foreground: function(e) {
    if (e.target.id === "") {
        this.findClickedElement(e, "li");
        return false;
    }

    console.log(e.target.id); // this returns the id which is '115'
    ... more code
}

如何抓取li上的数据属性如下:

<li class="item" id="115" data-foreground="blue" />

例如在 vanillaJS 中使用 getAttribute()

e.target.getAttribute('data-foreground');

或使用 dataset,(访问时不带 data- 前缀)(MDN)

e.target.dataset.foreground