我正在尝试使用PURE JAVASCRIPT检查数组中的匹配项。我不知道该怎么做,我会感谢你的帮助。
var sites = new Array ("site1.com", "site2.com", "site3.com" ...);
// Sites array contains 100 values
var imgs = document.getElementsByTagName("img");
for (var i = 0; i < imgs.length; i++) {
img = imgs[i].src;
// I'm trying to check if is in array,
// and don't waste a lot of size in code
if(img.match(sites)){
notHere(imgs[i]);
}
// This is the working way.
// Even if location is a.site1.com/b/, it will match
if (img.match("site1.com")) {
heReload(imgs[i]);
}
// Repeat this piece of code 100 times
}
}
注意:我不想检查确切的值。我想模拟这个match()
函数,所以如果 img = "http://abc/d/" 并且在数组中是 "bc/",它会执行function()
。