有没有办法获取数据集的多个值?

Is there a way to get the dataset's multiple values?

<input type="file" data-custom="file" data-custom-icons="i-file i-upload" />

我想获取 data-custom-icons 的值,但用 space 分隔它们。

// PREFERRED OUTPUT: ["i-file", "i-upload"]

我知道如何基本上获取值本身并尝试通过 Array.from() 将它们放入数组中,就像我对 classList 所做的那样。但是当我为那个数据集做这件事时,我的输出是每个字母对象。

var icons = el.dataset.customIcons;
console.log(icons);
var ic = Array.from(icons);
console.log(ic);

// WRONG OUTPUT: ["i", "-", "f", "i", "l", "e", " ", "i", "-", "u", "p", "l", "o", "a", "d"]

// Converting the classList in to an object
var cl = Array.from(el.classList);
console.log(cl);
OUTPUT: ["btn","btn-primary"]

数据集如此表现是否有原因?

在这种情况下,您需要使用带有所需分隔符的 split() 方法:

console.log( document.querySelector('input[type="file"]').dataset.customIcons.split(/\s+/) );
<input type="file" data-custom="file" data-custom-icons="i-file i-upload" />