2016-05-20 72 views
0

我想從一個字符串創建路徑,像:jQuery的分裂和各

var str = 'home/files/'; 
var crumbs = []; 
crumbs = str.split('/'); 
var nav; 
$.each(breadcrumbsUrls, function(u, i) { 
    if (i.length !== -1) { 
     nav += '<a href="'+u+'"></a> ' + i + ' <span class="arrow">→</span> '; 
    } 
}); 

,但我一直在結束了:

undefinedhome → files → → 

的結果應該是:

home → files 

回答

1

試試這個:

var str = 'home/files/'; 
var crumbs = []; 
crumbs = str.split('/'); 
crumbs = jQuery.grep(crumbs, function(n, i){ // remove all empty values from array using this 
    return (n !== "" && n != null); 
}); 
var nav = ''; 
$.each(crumbs, function(u, i) { 
    if((crumbs.length-1) != u) { // if not last record - add arrow 
     nav += '<a href="'+u+'"></a> ' + i + ' <span class="arrow">→</span> '; 
    } else { 
     nav += '<a href="'+u+'"></a> ' + i; 
    } 
}); 
+0

謝謝你的作品完美。 – Alko

0
var str = 'home/files/'; 
var crumbs = []; 
crumbs = str.split('/'); 
var nav = ''; // changed 
$.each(breadcrumbsUrls, function(u, i) { 
    if (i.length >0) { // changed 
    nav += '<a href="'+u+'"></a> ' + i + ' <span class="arrow">→</span> '; 
    } 
});