jquery 如何删除表格单元格中的文本,同时保留相邻元素?

pkwftd7m  于 2024-01-07  发布在  jQuery
关注(0)|答案(1)|浏览(214)

此问题在此处已有答案

How can I remove the text but not the child elements?(6个回答)
How to remove text (without removing inner elements) from a parent element using jQuery?(4个答案)
How to remove text that is not under any element with JavaScript(2个答案)
Remove text with jQuery(4个回答)
上个月关门了。
我有这个HTML

  1. <table>
  2. <tr>
  3. <td> <select id="foo">
  4. <option value="0" selected>0</option>
  5. <option value="1" >1</option>
  6. </select>
  7. pcs
  8. </td>
  9. </tr>
  10. </table>

字符串
我需要用jQuery删除<select\>后面的“pcs”字符串。我该怎么做?

  1. mmjQuery("#foo").parent().text(mmjQuery("#foo").parent().text().replace('db', ''));


但是使用该代码可以操作select DOM。

qlckcl4x

qlckcl4x1#

你可以做的是保存select元素到一个let,然后使用html()替换父td的内容:

  1. // Save the original select element here
  2. let select_elem = $('#foo');
  3. // use jQuery's html() method to pass the orignal select to overwrite the parent <td> content.
  4. $('#foo').parent('td').html(select_elem);

个字符

相关问题