如何替换“&书信电报;br/>&书信电报;br/>”;带有“&书信电报;br/>”;在HTML中使用JavaScript和jQuery?
我有一个验证摘要,有时在末尾有多个如何替换“&书信电报;br/>&书信电报;br/>”;带有“&书信电报;br/>”;在HTML中使用JavaScript和jQuery?,jquery,Jquery,我有一个验证摘要,有时在末尾有多个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 o
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>
$('#ValidationSummary1').replace('<br /><br />', '<br />');
我知道没有replace()
函数。除了像这样简单的东西,还有什么替代方法呢?JavaScript本身有一个替换函数,鉴于jQuery是一个基于JavaScript构建的框架,您确实可以使用该方法。因此,这意味着您必须执行一个get-operate-set过程,而不是一些很好的jQuery方法链接来完成它。使用.html()
通过标准的JSreplace()
获取和设置传递它的内容
var div=$('ValidationSummary1');
div.html(div.html().replace('
','
');
var$val=$('ValidationSummary1');
$val.html($val.html().replace(/[
]+$/,“
”);
无论
的数量是多少,它们都将被一个
(更好的html)替换。在您的情况下(需要删除html标记),最好的解决方案是使用.text()
而不是.html()
。区别在于.html()
保留html标记,而.text()
只保留文本(删除任何html)
例如:
var value1 = $('.some_div').html(); //returns text content with HTML tags
var value2 = $('.some_div').text(); //returns only text content
可能重复:尝试以下操作:$(“#ValidationSummary1”).replace(“
,”
,“
”);
@Asdfg-jQuery本身没有.replace()函数,这就是我发布此问题的原因。
var $val= $('#ValidationSummary1');
$val.html($val.html().replace(/[<br>]+$/, '<br/>');
var value1 = $('.some_div').html(); //returns text content with HTML tags
var value2 = $('.some_div').text(); //returns only text content