0

このタイプの質問はおそらく以前に尋ねられたことがありますが、作業に必要な特定のバリエーションが得られません。jquery を使用して、ユーザーの操作なしで、ページが最初に読み込まれたときに出力 html を自動的に変更する必要があります。

だから、私は変更できないこのhtmlにこだわっています:

<ul>
  <li><a href="A" class="fullProfile">View Full Profile</a></li>
  <li><a href="author" class="extLink">Laboratory Page</a></li>
</ul>

上記のすべてを次のように変更する必要があります。

<a name="A" class="letter">A</a>

ご想像のとおり、最終的にはこれを 26 回繰り返す必要があります (したがって、すべての文字にアンカー リンクを用意しています)。簡単に繰り返すことができる、軽量で効率的なものがあれば素晴らしいでしょう。

<head>私は間違いなくjqueryに精通していないので、ドキュメントに何を入れる必要があるかを示すjsfiddleまたは同様のものが理想的です!

ありがとう、ダル。

4

4 に答える 4

1

.fullProfileすべてのリンクをターゲットにし、 を取得します。hrefそれがアンカーの名前であるように思われるため、最も近いラッピングULを新しいアンカーに置き換えます。

$('.fullProfile').each(function() {
    var new_anchor = $('<a />', {name   : $(this).attr('href'), 
                                 'class':'letter',
                                 text   : $(this).attr('href')
                                });
    $(this).closest('ul').replaceWith(new_anchor);
});
于 2013-07-08T13:04:29.890 に答える
1

ライブデモ

$(function() {
    $(".fullProfile").each(function() {
      var href= $(this).attr("href");
      var link = $('<a name="'+href+'" class="letter">'+href+'</a>');
      $(this).closest("ul").replaceWith(link);
    });
});

アップデート

新しいアンカーを指すように他のリンクを処理します。

ライブデモ

$(function() {
  $(".fullProfile").each(function() {
    var href = $(this).attr("href");
    var link = $('<a name="'+href+'" class="letter">'+href+'</a>');
    $(this).closest("ul").replaceWith(link);
  });
  $(".cwyFellowName a").each(function() {
    var href = $(this).attr("href");
    if (href.length==1) $(this).attr("href","#"+href);
  });
});

アップデート

ここでは、独自の文字を持たない残りのリンクを扱います

 $(function() {
    $(".fullProfile").each(function() {
      var href = $(this).attr("href");
      var link = $('<a name="'+href+'" class="letter">'+href+'</a>');
      $(this).closest("ul").replaceWith(link);
    });
    $(".cwyFellowName a").each(function() {
      var href = $(this).attr("href");
      if (href.length==1) $(this).attr("href","#"+href);
      else if (href=="link") {
        var txt = $(this).text();
        if (txt.length==1) {
          $(this).attr("href","#"+txt);
          var nextDiv = $(this).parent().nextAll(".cwyFellowLinks:first");
          nextDiv.find("a").attr("name",txt).text(txt);
        }
      }    
   });
});
于 2013-07-08T13:09:37.883 に答える