我有不同的句子,其中都有双引号,例如:
<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>
说实话,我有点困惑应该使用哪个 jQuery 函数,在 split 和 text 之间?任何帮助将不胜感激,我需要了解如何做到这一点...非常感谢!
您可以匹配<h3>
元素,然后将函数传递给html() http://api.jquery.com/html/。该函数将为每个元素调用,将传递当前元素的内部 HTML 标记,并且必须返回新标记。
从那里,您可以使用代替() https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/replace插入一个<br />
第一个双引号字符之前的元素:
$("h3.myClass").html(function(index, currentHtml) {
return currentHtml.replace('"', '<br />"');
});
您可以在以下位置测试此解决方案这把小提琴 http://jsfiddle.net/yXUcw/.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)