我有一段服务器生成的html (我不能直接编辑html,所以修改必须是javascript/jquery) &我需要去掉一点文本+一个变量号,但保留所有其他内容。这是我的html:
<td>
<font class="carttext">
[Specifications:30][Specifications:
<br> These are other specifications: here & here
<br>And even more: because life is hard]
</font>
</td>请注意,[Specifications:30]可以是[Specifications:40]、[Specifications:90]或[Specifications:120]等,但总是以[Specifications:开头,以变量number & ]结尾
下面是我不工作但最努力的jquery:
var cartText = $(document.getElementsByClassName("carttext"));
cartText.html(function (index, html) {
return html.replace("[Specifications:" + /[0-9]+\]/, '');
});也曾尝试过:
var cartText = $(document.getElementsByClassName("carttext"));
cartText.html(function (index, html) {
return html.replace("[Specifications:" + /d +\]/, '');
});在"[Specifications:"类中有多个carttext事件,所以我只想去掉字符串为"[Specificaitons:(variable number here)"的事件
更新:我不仅试图删除数字,而且还删除[Specifications:,因此:
<font class="carttext">
[Specifications:30][Specifications: <br> These are other
specifications: here & here <br>And even more: because life is hard]
</font>变成了
<font class="carttext">
[Specifications:<br> These are other specifications: here & here
<br>And even more: because life is hard]
</font>很抱歉之前没有指定
发布于 2015-11-20 18:14:05
/分隔。[是regex中的特殊符号,因此需要使用//进行转义。g-global标志替换所有事件在页面上加载jQuery时,请使用html(),如下所示。
$('.carttext').html(function(i, oldHtml) {
return oldHtml.replace(/\[Specifications:\d+\]/g, '');
});<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<font class="carttext">
[Specifications:30][Specifications:
<br /> These are other specifications: here & here
<br />And even more: because life is hard]
</font>
https://stackoverflow.com/questions/33833351
复制相似问题