Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 如何删除html标记并用另一个标记包装它_Jquery_Html - Fatal编程技术网

Jquery 如何删除html标记并用另一个标记包装它

Jquery 如何删除html标记并用另一个标记包装它,jquery,html,Jquery,Html,我试图删除表格单元格下的某些元素 我的html就像 <table> <tr> <td> <span>first text</span> <span>second text</span> <span>third text</span> </td> <td> <span>1st text</span

我试图删除表格单元格下的某些元素

我的html就像

<table>
<tr>
  <td>
     <span>first text</span>
     <span>second text</span>
     <span>third text</span>
  </td>
   <td>
     <span>1st text</span>
     <span>2nd text</span>
     <span>3rd text</span>
   </td>
</tr>
…more

第一个文本
第二文本
第三文本
第一文本
第二文本
第三文本
……更多
我希望我的html成为

<table>
<tr>
  <td>
     <span>first text second text third text</span>
  </td>
   <td>
     <span>1st text 2nd text 3rd text</span>
   </td>
</tr>
…more

第一段文字第二段文字第三段文字
第1文本第2文本第3文本
……更多
我试过了

$('table td span').contents().unwrap().wrap('<span></span>');
$('table td span').contents().unwrap().wrap(“”);
但我得到的结果和我原来的一样


有办法做到这一点吗?非常感谢

需要迭代每个
td
并使用
wrapAll
而不是
wrap

$('table td').each(function() {
    $(this).find('span').contents().unwrap().wrapAll('<span></span>');
})
$('table td')。每个(函数(){
$(this.find('span').contents().unwrap().wrapAll('');
})

我认为您应该能够执行类似.text()的操作,然后将其包装在一个span中。(那么您应该改为使用$('table td'))我对它所做的唯一更改是
$(this).find('span').append(“”)
,以便在删除
后文本之间有一个空格。