1

次のコードは機能しますが、同じ結果を達成するためのよりコンパクトな方法、特に文字列の置換があると確信しています。

$('#nav-main .nav-sub').each(function() {

    var name = $(this).attr('id');

    $(this).children('li').children('a').each(function() {

        var text = $(this).text().toLowerCase();
        var spaces = / /g;
        var sub = /sub-/;
        var id = name + '-' + text.replace(spaces, '-');    
        var id = id.replace(sub, '');       
        $(this).attr('id', id);

    });

});
4

1 に答える 1

1
$('#nav-main .nav-sub').each(function() {
    var name = $(this).attr('id');
    $('li a', this).each(function() {       
        this.id = (name + '-' + $(this).text().toLowerCase().replace(/\s/g, '-')).replace(/sub-/, '');
    });
});
于 2009-02-10T13:46:03.973 に答える