2016-07-18 6 views
0

私は再びここにいるここで問題変更タグjqueryのを使用して属性に基づいて、名前とテキストエリア

を求めて私のテキストである私のtextarea

<Abstract> 
    <Heading>Abstract</Heading> 
    <Para TextBreak="No">Some paragraph <Emphasis Type="Italic">q</Emphasis> </Para> 

</Abstract> 

可能な出力から

<Abstract> 
    <Heading>Abstract</Heading> 
    <P>Some paragraph <i>q</i> </P> 
</Abstract> 

これが理由です出力の

テキストはテキスト領域に由来します。

iは、その属性に基づいてタグ名を変更する必要が

すべてのパラタグiが解決

検索

、すべての

Type="Italic" attribute must be change to <i> tag 

Pタグに変更する必要があります

しかし、私が見つけた解決策はhtmlを変更することです。テキスト領域の内容ではありません。 ありがとうそんなにこれは私が見つけた解決策ですが、私はそれは私の問題ここ

$(function(){ 
    $('#switch').bind('click', function(){ 
     $('p').replaceWith($('<div/>').html($('p').html())); 
    }); 
}); 

私は必要なもののサンプルがあるに動作させることはできません。あなたは

https://jsfiddle.net/bc6xequ7/

答えて

1

以下のようにそれをやってみてください感謝。タグ<para>type="Italic"の中のあなたの属性は削除されません。しかし、それは間違いなくあなたのタグを置き換えます。

jQuery(document).ready(function(){ 
 
textval = $('textarea').val(); 
 
textnewval = textval.replace('Para TextBreak="No"', 'p').replace('/Para', '/p'); 
 
    
 
    if(textnewval.indexOf('Italic') >= 0) //If Italic 
 
{ 
 
\t EmphasisAttr = 'Italic'; 
 
    textnewval = textnewval.replace('Emphasis Type="'+EmphasisAttr+'"', 'i').replace('/Emphasis', '/i'); 
 
} 
 
if(textnewval.indexOf('Bold') >= 0) //If Bold 
 
{ 
 
\t EmphasisAttr = 'Bold'; 
 
    textnewval = textnewval.replace('Emphasis Type="'+EmphasisAttr+'"', 'b').replace('/Emphasis', '/b'); 
 
} 
 
if(textnewval.indexOf('Underline') >= 0) //If underline 
 
{ 
 
\t EmphasisAttr = 'Underline'; 
 
\t textnewval = textnewval.replace('Emphasis Type="'+EmphasisAttr+'"', 'u').replace('/Emphasis', '/u'); 
 
} 
 
    
 

 
    $('textarea').val(textnewval); 
 
    alert($('textarea').val()); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<textarea rows="20" > 
 
    <Abstract> 
 
    <Heading>Abstract</Heading> 
 
    <Para TextBreak="No" >Some paragraph <Emphasis Type="Italic">q</Emphasis><Emphasis Type="Bold">Bold</Emphasis><Emphasis Type="Underline">Underline</Emphasis> </Para> 
 

 
</Abstract> 
 
</textarea>

それはあなたが期待するものではない場合、私に教えてください。

+0

動作しません。終了タグのタグ名は変更されませんでした。 – askquestionzero

+0

このような終了タグの置換メソッドを追加することができます。 '.replace( '/ Para'、 '/ p')' –

+0

が動作することがあります。 emphasisタグにはBold属性が含まれています。特定のタグの属性をチェックしてタグ名を変更する方法はありますか? – askquestionzero

関連する問題