gpt4 book ai didi

javascript - jQuery:如何在不同的字符串中添加一个
换行符来分隔2中的句子?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:26:36 27 4
gpt4 key购买 nike

我有不同的句子,里面都有双引号,比如:

<h3 class="myClass">Sentence one "ends like this"</h3>
<h3 class="myClass">Sentence two"ends like that"</h3>
<h3 class="myClass">Sentence three "another ending"</h3>

全部在一个页面上。基本上所有的值都是不同的,我试图在双引号之前换行,这样就好像

<h3 class="myClass">Sentence one <br/>"ends like this"</h3>
<h3 class="myClass">Sentence two <br/>"ends like that"</h3>
<h3 class="myClass">Sentence three <br/>"another ending"</h3>

老实说,在 split 和 text 之间,我有点困惑应该使用哪个 jQuery 函数?任何帮助将不胜感激,我需要了解如何执行此操作...非常感谢!

最佳答案

您可以匹配 <h3>元素,然后将函数传递给 html() .该函数将为每个元素调用,将传递当前元素的内部 HTML 标记,并且必须返回新标记。

从那里,您可以使用 replace()插入 <br />第一个双引号字符之前的元素:

$("h3.myClass").html(function(index, currentHtml) {
return currentHtml.replace('"', '<br />"');
});

您可以在 this fiddle 中测试此解决方案.

关于javascript - jQuery:如何在不同的字符串中添加一个<br/>换行符来分隔2中的句子?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12062179/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com