【发布时间】:2015-01-19 13:38:59
【问题描述】:
您好,我正在尝试几个小时来在添加文本字符串后再次删除它。
我有一个处理手风琴的脚本,其中的文本有些冗余。所以我想在打开或关闭手风琴行时添加和删除多余的文本。
这是我的代码:
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() 不能。因此,如果我多次打开和关闭手风琴行,它总是会附加冗余文本,但从不删除它,从而使冗余文本变得更加冗余。
编辑: 将“冗余文本”更改为冗余文本。还是不行
编辑2:
$("#redundant_text_wrapper").text($("#redundant_text_wrapper").text().replace(redundantText,''));
也无济于事,它只会删除链接但文本仍然存在
编辑3:
$( "#redundant_text_wrapper").text(function(index, currentText) {
return currentText.replace(redundantText,'');
});
也只是删除链接,文本保留
【问题讨论】:
-
将
$( "#redundant_text_wrapper").text().replace('redundantText','');更改为:$( "#redundant_text_wrapper").text().replace(redundantText,''); -
@SuperDJ 把它作为答案。 :)
-
@BhojendraNepal 更改使用包含文本的变量。目前的方式是在text()中搜索
redundantText,不存在。 -
前面的冗余文本被认为是s字符串..
-
您正在替换文本,但对结果不做任何事情
标签: javascript jquery appendtext