2010-12-21 8 views
2

jQueryを使用すると、<acronym>タグをすべて<abbr>に置き換えるとどうでしょうか?jQueryを使用したHTML5略語タグの置き換え

+0

ないあなたはjQueryの、またはJavaScriptの完全停止でこれをしたいと思う理由を確認してください。正しいマークアップをHTML文書に書き込むか、XMLまたはJSON(サーバー側)の用語集を保持し、HTMLの用語テキストノードのインスタンスを省略タグでラップして変換します。 – DigiKev

答えて

3
$('acronym').each(function() { 
    var $this = $(this); 
    $this.before('<abbr>' + $this.html() + '</abbr>'); 
    $this.remove(); 
}); 
+0

私よりも簡単です。それはすべての場合の99%のように機能するので、+1 – Blender

+2

あなたは、あまりにも任意の属性をコピーしたいでしょう:http://stackoverflow.com/questions/2048720/get-all-attributes-from-a-html-element -with-javascript-jquery –

2

私はこれを試したいと思いますが、私は保証をしません。

$('acronym').each(function(index) 
{ 
    var old = this; 
    var newElement = $("<abbr></abbr>"); 

    $.each(this.attributes, function(index) 
    { 
    $(newElement).attr(old.attributes[index].name, old.attributes[index].value); 
    }); 

    $(newElement).html(old.html()); 

    $(this).after(newElement).remove(); 
}); 

幸運、そしてそれはbrokededなら教えてください。もしそれが犯されたら、私はそれを修正しようとします。

+0

元のタグの内容をコピーするのを忘れました。 – sje397

+0

これで、属性ごとにコンテンツを1回コピーしますか? – sje397

+0

年。私はそれが正しいとは思わない... – Blender

1

jQueryのreplaceWithを使用します。私はtitle属性のみを使用していると仮定しています。

$("acronym").each(function(){//for each acronym element 
     var abbr = $("<abbr></abbr>");//create a new abbr element 
     var title = $(this).attr("title");//get the title attribute from the acronym element 
     abbr.attr("title",title);//add it to the new abbr element 
     abbr.html($(this).html());//add in the content of the acronym element 
     $(this).replaceWith(abbr);//replace the old with the new! 
    }); 

Try it!

1

の線に沿って何か:

$('acronym').each(function(i, el) { 
    $(this).replaceWith($('<abbr></abbr>').html($(this).html())); 
}); 

http://jsfiddle.net/7H4G6/1/

編集:あなたはどんな...当たり前のかかっている場合は、あまりにも属性をコピーしたいと思いますあなたはそうしない。

  • クリスチャン
1
$('acronym').each(function() { 
    $(this).replaceWith('<abbr>' + $(this).html() + '</abbr>'); 
}); 
1
$("acronym").each(function(idx,HTML) { 
    $(this).replaceWith('<abbr title="'+this.title+'">'+HTML+'</abbr>'); 
}); 
関連する問題