2016-12-08 5 views
2

でデルを見つけ、私はこのHTMLINSタグにすべてのINS、テキストノードラップを見つけ、新しいDELタグのそれぞれのクラス名

<html> 
    <head></head> 
<body> 
    <div class="para"> 
     <ins>A computer is a</ins> popular <del>new</del><ins> device</ins> 
    </div> 

    <div class="para"> 
     <ins>Stored programs</ins><del>required the re-wiring and re-structuring of the machine.</del> 
    </div> 
    <div class="para"> 
     <ins>Integrated </ins>new<del>circuits</del><ins>System </ins> 
    </div> 
</body> 
</html> 

を持っている私は、すべてのインをつかむしたい、とそれぞれのテキストノードパラクラスと新しいインタグにラップしても、次の

<html> 
<head></head> 
<body> 
    <div class="para"> 
     <ins>A computer is a popular</ins><del>new</del><ins> device</ins> 
    </div> 

    <div class="para"> 
     <ins>Stored programs</ins><del>required the re-wiring and re-structuring of the machine.</del> 
    </div> 
    <div class="para"> 
     <ins>Integrated new</ins><del>circuits</del><ins>System </ins> 
    </div> 
</body> 
</html> 

のような新しいデルタグにデルタグiは、次のコードを試してみました

var unit = $('.para'); 
$.each(unit, function(){ 
    var nodes = $(this).find('ins'); 
}) 

これを解決するのに役立ちます

+0

なぜ 'ない期待される結果で' html'で ' "popluar" ですか?に選択#textノードを追加する.appendTo()また、結果の最初の '.para'は、' 'で始まっていないか、余分な' 'がありますか? – guest271314

答えて

2

現在の.paraの要素に等しい.parentElementを持って.para要素内#textノードを取得.each().contents().filter()を使用することができます。現在.para要素内の最初ins要素

$(".para").each(function(index, element) { 
 
    $(this).contents().filter(function() { 
 
    return this.nodeName === "#text" && this.parentElement === element 
 
    }).appendTo($("ins:first", element)) 
 
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<html> 
 

 
<head></head> 
 

 
<body> 
 
    <div class="para"> 
 
    <ins>A computer is a</ins> popular <del>new</del><ins> device </ins> 
 
    </div> 
 

 
    <div class="para"> 
 
    <ins>Stored programs</ins><del>required the re-wiring and re-structuring of the machine.</del> 
 
    </div> 
 
    <div class="para"> 
 
    <ins>Integrated </ins>new<del>circuits</del><ins>System </ins> 
 
    </div> 
 
</body> 
 

 
</html>

0

これらの配列を取得するには、を使用することができます。そして、それらの子供をターゲットにしてループします。

jqueryを使用して死んだ場合や、いくつかのバニラに満足している場合は、jqueryではありません。

関連する問題