我一直在浏览一些代码并将所有jQuery转换为vanilla JS。下面是我成功转换的一些代码(注释掉是jQuery)。在我最后添加[0]之前,我遇到了一些麻烦。我知道在访问数组时会使用方括号,但我不确定为什么在这个实例中需要它们。有人可以解释一下吗?
var head = document.getElementsByTagName('head')[0];
var linkScript = document.createElement('link');
linkScript.type = 'text/css';
linkScript.rel = 'stylesheet';
linkScript.href = purecommHostFiles + 'style.css';
head.appendChild(linkScript);
// $('<link>')
// .appendTo('head')
// .attr({
// type: 'text/css',
// rel: 'stylesheet',
// href: purecommHostFiles + 'style.css'
// });