嗨,我正在尝试几个小时来在添加文本字符串后再次删除它。
我有一个处理手风琴的脚本,其中的文本有些冗余。所以我想在打开或关闭手风琴行时添加和删除多余的文本。
这是我的代码:
var redundantText = "text text text <a href=\"#contact\">Contact Me</a> text text text";
$('#collapse_1').on('show.bs.collapse', function() {
$(".dropdown_collapse_1").addClass("dropdown-indicator");
$(".dropdown_collapse_1").removeClass("dropdown-collapsed");
$("#redundant_text_wrapper").append(redundantText);
});
$('#collapse_1').on('hide.bs.collapse', function() {
$(".dropdown_collapse_1").addClass("dropdown-collapsed");
$(".dropdown_collapse_1").removeClass("dropdown-indicator");
$("#redundant_text_wrapper").text().replace(redundantText, '');
});
附加工作正常,但 text().replace() 没有。因此,如果我多次打开和关闭手风琴行,它总是会附加冗余文本,但从不删除它,这样冗余文本就会变得更加冗余。
编辑:将“redundantText”更改为redundantText。还是不行
编辑2:
$("#redundant_text_wrapper").text($("#redundant_text_wrapper").text().replace(redundantText,''));
也无济于事,它只会删除链接但文本仍然存在
编辑3:
$( "#redundant_text_wrapper").text(function(index, currentText) {
return currentText.replace(redundantText,'');
});
也只删除链接,文本保留