2

私はこのコードを持っています:

<td class="tabledata">
  <img>
  <span class="someclass"></span>
  <br> text
</td>

テキストを取得して div でラップし、クラス名を付けたいと思います。

私はもう試した:

$(".tabledata").html().split("<br />")[1];

$(".tabledata").each(function() {
  $(this).nextAll("br").get(0).wrap('<div></div>')
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<td class="tabledata">
  <img>
  <span class="someclass"></span>
  <br> text
</td>

4

1 に答える 1

4

試す

$($(".tabledata").children('br').get(0).nextSibling).wrap('<div />');

デモ:フィドル

td のすべてのダイレクト テキスト ノードの子をラップする場合は、

$(".tabledata").contents().filter(function(){
    return this.nodeType == 3 && $.trim($(this).text()) != '';
}).wrap('<div />');

デモ:フィドル

于 2013-08-16T09:34:18.037 に答える