2015-03-13 52 views
-1

所以我有一个函数为每个'#tree ol li'启动一个函数树是从php脚本生成的文件和目录的列表。 my认为是否'#tree ol li a'锚元素的值以特定的扩展名结束并隐藏它。所以我试图让这个脚本..但它不工作.. 谁能帮我找到我在哪里错了当场..隐藏链接,如果它匹配一个数组的值

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(); 
}; 
}); 
+0

我知道我的循环有点搞砸了,但我故意这样做。 – 2015-03-13 14:08:12

+0

你能指定它不工作多远吗? – Burki 2015-03-13 14:09:52

+0

一切似乎很好,直到console.log(temp_val2);因为它正在触发正确的值。 – 2015-03-13 14:11:04

回答

0

试试这个:

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(); 
    }; 
    }); 
1

function checkstuff() { 
 
    $("#tree ol li").each(function() { 
 

 

 
    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"]; 
 
    for (i = 0; i < extArr.length; i++) { 
 
     if (temp_val2 == extArr[i]) { 
 
     console.log(extArr[i] + "WELL this should have worked.."); 
 
     $(this).hide(); 
 
     } 
 
    } 
 
    }); 
 
}

这是你的工作脚本

+0

谢谢!有用!它为我节省了很多头痛,我很欣赏它! – 2015-03-13 14:27:47

1

这是你的循环,打破你的代码。 让我们假设你有3个环节

x.wav 
y.mp3 
z.m4a 

您正在搜索的方式是在一个特定的顺序为:MP3,WAV,M4A,然后。你想要做的是搜索,看看你的结果包含在阵列中,而不是是否与索引搜索重合

(即:第一HREF MUST在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(); 
     }; 
    }); 
+0

哦,一个简单的错误打破了一切..但感谢您的帮助! – 2015-03-13 14:30:02

相关问题