4

初めての Chrome 拡張機能を作成しようとしています。基本的に、現在読み込まれているドキュメントでクラス名が「子」のすべてのリンクを検索したい。各リンクの href 値を取得する必要があります。次に、「child_two」という名前の別のクラスにクローンしたいと思います。新しく複製された要素では、href が変更されたものに置き換えられます。

これが対象文書です。

<div class="parent">
    <a href="http://www.example1.com" class="child">name1</a>
</div>
<div class="parent">
    <a href="http://www.example2.com" class="child">name2</a>
</div>
<div class="parent">
    <a href="http://www.example3.com" class="child">name3</a>
</div>
<div class="parent">
    <a href="http://www.example4.com" class="child">name4</a>
</div>
<div class="parent">
    <a href="http://www.example5.com" class="child">name5</a>
</div>

こんな結果になってほしい。

<div class="parent">
    <a href="http://www.example1.com" class="child">name1</a>
    <a href="preview.php?link=http://www.example1.com" class="child_two">preview</a>
</div>
<div class="parent">
    <a href="http://www.example2.com" class="child">name2</a>
    <a href="preview.php?link=http://www.example2.com" class="child_two">preview</a>
</div>
<div class="parent">
    <a href="http://www.example3.com" class="child">name3</a>
    <a href="preview.php?link=http://www.example3.com" class="child_two">preview</a>
</div>
<div class="parent">
    <a href="http://www.example4.com" class="child">name4</a>
    <a href="preview.php?link=http://www.example4.com" class="child_two">preview</a>
</div>
<div class="parent">
    <a href="http://www.example5.com" class="child">name5</a>
    <a href="preview.php?link=http://www.example5.com" class="child_two">preview</a>
</div>

どうもありがとうございます。

4

3 に答える 3

0
$("a.child").each(function(){
    var newLink = $(this).clone();
    $(newLink).attr("href", "preview.php?link=" + $(this).attr("href"));
    $(newLink).attr("class", "child_two");
    $(newLink).text("Preview");
    $(newLink).insertAfter(this);
});

http://jsfiddle.net/bkU4r/2/

更新 - 2 番目のリンクに「プレビュー」を表示する必要があることに気付きませんでした

于 2012-12-21T22:34:56.600 に答える