我有验证摘要,有时在末尾有多个br
标签,如下所示:
<div id="ValidationSummary1" class="alert alert-error">
Last 4-digits of Social Security number does not match
the Member record on file for the Member ID number you
have entered.<br />
Date of Birth does not match the Member record on file for the Member ID
number you have entered.<br /><br />
</div>
我试图找出任何时候有两个背靠背的br
标签,并将其替换为如下所示:
$('#ValidationSummary1').replace('<br /><br />', '<br />');
我知道这里没有replace()
函数。像这样简单的东西有什么替代方案呢?
发布于 2013-02-08 15:31:13
使用.html()
获取并设置通过标准JS replace()
传递的内容。
var div = $('#ValidationSummary1');
div.html(div.html().replace('<br /><br />', '<br />'));
发布于 2013-02-08 15:30:47
JavaScript本身确实有一个replace
函数,并且鉴于jQuery是一个构建在JavaScript上的框架,您确实可以使用该方法。因此,这意味着你将不得不做一个获取-操作-设置过程,而不是一些很好的jQuery方法链接来完成它。
发布于 2013-02-08 15:38:10
var $val= $('#ValidationSummary1');
$val.html($val.html().replace(/[<br>]+$/, '<br/>');
无论数量是多少
它们将被替换为一个
(更好的html)
https://stackoverflow.com/questions/14775894
复制