Javascript 如果链接与数组的值匹配,则隐藏链接

Javascript 如果链接与数组的值匹配,则隐藏链接,javascript,jquery,Javascript,Jquery,所以我有一个函数,fire是每个“#tree ol li”的函数,tree是由php脚本生成的文件和目录的列表。。 我的想法是查看锚元素的值是否以特定的扩展名结尾,并将其隐藏。所以我试着制作这个脚本。。但是它不起作用。。 谁能帮我找到我错的地方吗 function checkstuff(){ i = 0; $("#tree ol li").each(function(){ i++; console.log(i); var temp_val = $(this).children("a").att

所以我有一个函数,fire是每个“#tree ol li”的函数,tree是由php脚本生成的文件和目录的列表。。 我的想法是查看锚元素的值是否以特定的扩展名结尾,并将其隐藏。所以我试着制作这个脚本。。但是它不起作用。。 谁能帮我找到我错的地方吗

function checkstuff(){
i = 0;
$("#tree ol li").each(function(){

i++;
console.log(i);
var temp_val = $(this).children("a").attr("href");
var temp_val2 = temp_val.substr(temp_val.length - 4);
console.log(temp_val2);
var extArr = [".mp3",".wav",".m4a"];
if(temp_val2 == extArr[i]){
console.log(extAttr[i] + "WELL this should have worked..");
$(this).hide();
}; 
});
试试这个:

function checkstuff(){
    i = 0;
    $("#tree ol li").each(function(){

    i++;
    console.log(i);
    var temp_val = $(this).children("a").attr("href");
    var temp_val2 = temp_val.substr(temp_val.length - 4);
    console.log(temp_val2);
    var extArr = [".mp3",".wav",".m4a"];
    if($.inArray(temp_val2 ,extArr[i])>-1){
    console.log(extAttr[i] + "WELL this should have worked..");
    $(this).hide();
    }; 
    });
函数checkstuff(){
$(“#tree ol li”)。每个(函数(){
var temp_val=$(this.children(“a”).attr(“href”);
变量temp_val2=temp_val.substr(temp_val.length-4);
控制台日志(临时值2);
var extArr=[“.mp3”、“.wav”、“.m4a”];
对于(i=0;i}
是for循环破坏了代码。
假设你有3个链接

x.wav
y.mp3
z.m4a
您搜索的方式按特定顺序排列:mp3、wav,然后是m4a。您要做的是搜索以查看结果是否包含在数组中,而不是它是否与索引搜索一致

(即:第一个href必须以.mp3结尾)

要这样做,您希望这样做:

function checkstuff() {
    i = 0;
    $("#tree ol li").each(function() {

      i++;
      console.log(i);
      var temp_val = $(this).children("a").attr("href");
      var temp_val2 = temp_val.substr(temp_val.length - 4);
      console.log(temp_val2);
      var extArr = [".mp3", ".wav", ".m4a"];
      if (extArr.indexOf(tempval2) > -1) {
        console.log(extArr.indexOf(tempval2) + "WELL this should have worked..");
        $(this).hide();
      };
    });

我知道我的循环有点混乱,但我是故意这样做的。你能具体说明它不起作用的程度吗?在console.log(temp_val2)之前,一切似乎都很好;当它触发正确的值时。你说它触发正确的值是什么意思?你能展示一下你得到了什么,并解释一下你想要得到什么吗?如果链接的扩展名是.mp3或.wav或.m4a,你是想隐藏链接吗?谢谢!它起作用了!这让我省去了很多头痛,我很感激!哦,一个简单的错误打破了一切。。但是谢谢你的帮助!