4

4 つのリンクがあり、rel 属性の href 属性を変更する必要があります。私はそれができないことを知っているので、href 属性からデータを取得し、新しい属性 (rel) を設定し、その中にデータを挿入してから、href 属性を削除しようとしています。

基本的に私はこれをやっています:

$('div#menu ul li a').each(function(){
        var lin = $(this).attr('href');
        $('div#menu ul li a').attr('rel',lin);
        $(this).removeAttr('href');


        })
    })

それは機能しますが、私が持っているすべてのリンクに同じrelデータを設定します。

助けはありますか?

4

6 に答える 6

4
$('div#menu ul li a').each(function(){
    var lin = $(this).attr('href');
    $(this).attr('rel',lin);
    $(this).removeAttr('href');
});
于 2012-09-30T18:12:38.580 に答える
2

試す

$('div#menu ul li a').each(function(){
    var lin = $(this).attr('href');
    $(this).attr('rel',lin);
    $(this).removeAttr('href');


    })
})

実際にコードを実行していません...しかし、それはうまくいくはずです。

于 2010-04-16T15:50:59.113 に答える
2

変化する:

$('div#menu ul li a').attr('rel',lin);

に:

$(this).attr('rel',lin);
于 2010-04-16T15:51:56.497 に答える
0

あなたはおそらく「それ」を間違っています。(あなたが試みていることを行うためのより良い方法があることを意味します)。

しかし、あなたの質問に答えるだけです:

$('#menu ul li a').each(function(){
    var lin = $(this).attr('href');
    $(this).attr('rel',lin).removeAttr('href');
});
于 2010-04-16T15:52:21.640 に答える
0

これは堅実なjqueryソリューションです:

$(function() {
    $("a.selector").on("click", function() {
        var trigger = $(this.hash);
        trigger.removeAttr("id");
        window.location.hash = this.hash;
        trigger.attr("id", this.hash.replace("#",""));
        return false;
    });
});
于 2013-02-21T00:14:03.010 に答える