How can I put <br> tag in <td> contents with JQuery?
在我的
洛雷姆ipsum只是印刷和排版行业的一个虚拟文本。-.lorem ipsum自15世纪以来一直是业界标准的虚拟文本。-它不仅保存了5个世纪,而且还跨越到了电子排版。
我想要什么洛雷姆ipsum只是印刷和排版行业的一个虚拟文本。
-自15世纪以来,lorem ipsum一直是业界标准的虚拟文本。
-它不仅保存了五个世纪,而且还跃进了电子排版。
这是我的代码示例1 2 3 4 5 6 7 8 9 10 11 12 13 | <table class="Notice"> <thead> <tr> <th>Name</th> <th>Number</th> </tr> <thead> <tbody> <tr> <td>Lorem Ipsum is simply dummy text of the printing and typesetting industry. -.Lorem Ipsum has been the industry's standard dummy text ever since the 1500s. -.It has survived not only five centuries, but also the leap into electronic typesetting.</td> </tr> </tbody> </table> |
javascript
1 2 3 | $('td:contains("-.")').html(function (i, htm) { return htm.replace(/-./g,"-."); }); |
号解决方案
我发现了我的错误-我没有做到每一个字。所以我使用了
1 2 3 | $('td:contains("-.")').each(function () { $(this).html($(this).html().replace(/-./g,"-.")); }); |
试试这个:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <!DOCTYPE html> <html> <body> <p> Click the button </p> <p id="demo">Lorem Ipsum is simply dummy text of the printing and typesetting industry. -.Lorem Ipsum has been the industry's standard dummy text ever since the 1500s. -.It has survived not only five centuries, but also the leap into electronic typesetting. </p> <button onclick="myFunction()">Try it</button> function myFunction() { var text = document.getElementById("demo").innerHTML; text = text.replace(new RegExp("-.","g"),"-."); document.getElementById("demo").innerHTML = text ; } </body> </html> |
号
使用带有全局匹配的javascript replace()函数。
1 | replace(/-/g,"<br />-"); |