jquery text()。replace('','')不起作用 [英] jquery text().replace('','') not working
问题描述
您好我正在尝试几个小时后再添加文本字符串。
Hi I am trying for hours now to remove a text string again after I have appended it.
我有一个处理手风琴的脚本我有一些冗余在其中的文字。所以我想在打开或关闭手风琴行时添加和删除冗余文本。
I have a script that handles an accordion and I have some redundancy in text in it. So I want to add and remove the redundant text on opening or closing the accordion row.
这是我的代码:
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,但永远不会删除它,以便冗余文本变得更加冗余。
The append works fine but the text().replace() does not. So if I open and close the accordion row several times it always appends the redundantText but never removes it so that the redundantText gets even more redundant.
编辑:
将'redundantText'更改为redundantText。仍然无效
changed 'redundantText' to redundantText. Still does not work
Edit2:
$("#redundant_text_wrapper").text($("#redundant_text_wrapper").text().replace(redundantText,''));
也没有帮助,它只删除了链接,但文字保持
also does not help, it only removes the link but the text stays
Edit3:
$( "#redundant_text_wrapper").text(function(index, currentText) {
return currentText.replace(redundantText,'');
});
也只删除链接,文本保留
also does only remove the link, text stays
推荐答案
好的,我现在尝试了另一种方法:
Ok, I tried a different approach now:
var redundantText = "<span id=\"redundantText_wrapper\"> text text text <a href=\"#contact\">Contact Me</a> text text text</span>";
$('#collapse_1').on('show.bs.collapse', function() {
$(".dropdown_collapse_1").addClass("dropdown-indicator");
$(".dropdown_collapse_1").removeClass("dropdown-collapsed");
$("#redundant_text_wrapper").after(redundantText);
});
$('#collapse_1').on('hide.bs.collapse', function() {
$(".dropdown_collapse_1").addClass("dropdown-collapsed");
$(".dropdown_collapse_1").removeClass("dropdown-indicator");
$('#redundantText_wrapper').remove();
});
所以基本上我使用'after'而不是'append'并将文本放入带ID的范围内。现在我可以使用$('#ID')。remove();在关闭标签时摆脱它。
So basically I use 'after' instead of 'append' and put the text into a span with ID. Now I can use $('#ID').remove(); to get rid of it on closing the tab.
这篇关于jquery text()。replace('','')不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!