2015-03-13 53 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

相關問題