![](/img/trans.png)
[英]Using replace to remove <strong> tags from html
[英]Replace Html from <b> to <strong> with javascript and Remove <font> tag
我的代碼中都有這種類型的代碼
<td><font color="#3586DF"><b><span>1</span></b></font></td>
<td><font color="#3586DF"><b><a href="#"><font color="#3586DF">2</font></a></b></font></td>
我想用強替換b並刪除字體標簽
結果應該是
<td><strong><span>1</span></strong></td>
<td><strong><a href="#">2</a></strong></td>
我如何用jquery,javascript或CSS做到這一點
可以使用jQuery replaceWith()
$('font').replaceWith(function() { return $(this).contents() }) $('b').replaceWith(function() { return $('<strong>').append($(this).html()) }) console.log($('table').html())
strong, strong a { color: red }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tr> <td> <font color="#3586DF"><b><span>1</span></b></font> </td> <td> <font color="#3586DF"><b><a href="#"><font color="#3586DF">2</font></a></b></font> </td> </tr> </table>
您可以使用循環在表內找到b標簽,並像這樣替換它。
$('#testtable').find('tr > td > font > b').each(function() { $(this).replaceWith($('<strong>' + $(this).html() + '</strong>')); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table id='testtable'><tr> <td><font color="#3586DF"><b><span>1</span></b></font></td> <td><font color="#3586DF"><b><a href="#"><font color="#3586DF">2</font></a></b></font></td> </tr></table>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.