JavaScript获取数组中的最后一项

到目前为止,这是我的JavaScript代码:

var linkElement = document.getElementById("BackButton");

var loc_array = document.location.href.split('/');

var newT = document.createTextNode(unescape(capWords(loc_array[loc_array.length-2])));

linkElement.appendChild(newT);

当前,它需要URL中数组的倒数第二个项目。但是,我想检查数组中的最后一项index.html是否正确,如果是,请改为抓取倒数第三项。

回答:

if (loc_array[loc_array.length - 1] === ‘index.html’) {

// do something

} else {

// something else

}

如果您的服务器为“ index.html”和“ inDEX.htML”提供相同的文件,则您也可以使用:.toLowerCase()

但是,如果可能的话,您可能要考虑在服务器端进行此操作:它将更加干净并且可以在没有JS的情况下使用。

以上是 JavaScript获取数组中的最后一项 的全部内容, 来源链接: utcz.com/qa/427688.html

回到顶部