2017-10-30 19 views
0

によってスパンコンテンツを置き換える:jQueryのは、私はフォーマットを以下にHTMLマークアップを含む文字列を持っている文字列

<p> 
    <span class="xyz-display"> 
    <lots of other contents> 
    </span> 
</p> 

私は、文字列で衛生を実行する前に、文字列で<span class="xyz-display">内部のすべてを交換する必要があります。だから、以下のようになります。この後

<p> 
    <span class="xyz-display"> 
    abc 
    </span> 
</p> 

私は、元のコンテンツ<lots of other contents>バックでabcを交換する必要があります。どのようにjQueryを使ってこれを達成できますか?

おかげで、よろしく

+0

何か試しましたか? – Satpal

+1

$( "。xyz-display")。テキスト( "your_content"); –

答えて

0

<span>タグの内容を変更するには、これを試してみてください:

$("span.xyz.display").html("<lots of other contents>") 
0

私はボタンでデモを追加した

var a = document.querySelectorAll('span') 
for(var i=0; i<a.length; i++){ 
    a[i].whatever="" // to change and use the same method to change it back` 
0

を使用することができます。ここの主なコンセプトは

$(".xyz-class > span").html(what_to_add); 

です。実際にはxyz-classのインスタンスが必要です。さらに ">"はspan要素である子要素を表し、html(what_to_add)はHTMLコードをそのセクションに適用するために使用されます。

ここにそのコードがあります。

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <title>Hello world</title> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
    <script> 
     function add(){ 
      var what_to_add = '<p>Enter name </p> <input type="text" /> '; 
      $(".xyz-class > span").html(what_to_add); 
     } 
    </script> 
</head> 
<body> 
    <div> 
     <p class="xyz-class"> 
      <span>Hello world</span> 
     </p> 
     <button onclick="add()">Add</button> 
    </div> 
</body> 
</html> 

文字列を追加するには、必要な文字列にwhat_to_addを割り当てることができます。つまりwhat_to_add = "あなたの文字列" それがあなたを助けてくれることを願っています。

0

$(function(){ 
 
    $('.xyz-display').html('Hello! ') 
 
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<span class="xyz-display"> 
 
    abc 
 
</span>

これを試してみてください。

関連する問題