Javascript jquery text().replace(';';,';';)不工作

Javascript jquery text().replace(';';,';';)不工作,javascript,jquery,appendtext,Javascript,Jquery,Appendtext,嗨,我现在尝试了几个小时来删除一个文本字符串后,我已经附加了它 我有一个处理手风琴的脚本,里面有一些冗余的文本。所以我想在打开或关闭accordion行时添加和删除多余的文本 这是我的密码: var redundantText = "text text text <a href=\"#contact\">Contact Me</a> text text text"; $('#collapse_1').on('show.bs.collapse', function() {

嗨,我现在尝试了几个小时来删除一个文本字符串后,我已经附加了它

我有一个处理手风琴的脚本,里面有一些冗余的文本。所以我想在打开或关闭accordion行时添加和删除多余的文本

这是我的密码:

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, '');
}); 
也没有帮助,它只是删除链接,但文本保持不变

编辑3:

$( "#redundant_text_wrapper").text(function(index, currentText) {
    return currentText.replace(redundantText,'');
}); 
也只删除链接,文本保持更改

$( "#redundant_text_wrapper").text().replace('redundantText',''); 
致:

删除变量周围的引号。这样,它将被视为变量,而不是字符串

这样,它将首先获取文本,然后替换并设置它。

更改

$( "#redundant_text_wrapper").text().replace('redundantText',''); 
致:

删除变量周围的引号。这样,它将被视为变量,而不是字符串

这样,它将首先获取文本,然后替换文本并进行设置。

应该是:

$( "#redundant_text_wrapper").text( $( "#redundant_text_wrapper").text().replace('redundantText','') ) ;
或者,如果要替换所有引用:

$( "#redundant_text_wrapper").text( $( "#redundant_text_wrapper").text().replace(/redundantText/g,'') ) ;
这应该是:

$( "#redundant_text_wrapper").text( $( "#redundant_text_wrapper").text().replace('redundantText','') ) ;
或者,如果要替换所有引用:

$( "#redundant_text_wrapper").text( $( "#redundant_text_wrapper").text().replace(/redundantText/g,'') ) ;
$(“#redundant_text_wrapper”).text()

另外,由于
redundantText
包含html,您可能应该使用
html()
而不是
text()

如果要操作元素的现有html,则应使用的重载(还有)。这将接收元素的当前HTML并返回要设置的新HTML:

$( "#redundant_text_wrapper").html(function(index, oldHtml) {
    return oldHtml.replace(redundantText,'');
});

综上所述,替换包装器的全部内容效率较低,可能会破坏包装器中元素上的现有事件处理程序。我想说的是,最好将多余的文本放入另一个元素中,例如
,然后添加和删除它(或者只显示和隐藏它):

//将其全部包装在一个中-为其指定一个类以确保
var redundantText='text';
$('#collapse_1')。on('show.bs.collapse',function(){
$(“.dropdown\u collapse\u 1”).addClass(“下拉指示器”);
$(“.dropdown\u collapse\u 1”).removeClass(“dropdown collapse”);
$(“#冗余#文本#包装”).append(redundantText);
});
$('#collapse_1')。on('hide.bs.collapse',function(){
$(“.dropdown\u collapse\u 1”).addClass(“dropdown collapse”);
$(“.dropdown\u collapse\u 1”).removeClass(“下拉指示器”);
//只需找到跨度并将其移除即可
$(“#冗余#文本_包装器>span.redundant”).remove();
});
$(“#redundant_text_wrapper”).text().replace('redundantText',”)
只获取文本值,替换它,对结果不做任何处理,因此这行具有讽刺意味的冗余

另外,由于
redundantText
包含html,您可能应该使用
html()
而不是
text()

如果要操作元素的现有html,则应使用的重载(还有)。这将接收元素的当前HTML并返回要设置的新HTML:

$( "#redundant_text_wrapper").html(function(index, oldHtml) {
    return oldHtml.replace(redundantText,'');
});

综上所述,替换包装器的全部内容效率较低,可能会破坏包装器中元素上的现有事件处理程序。我想说的是,最好将多余的文本放入另一个元素中,例如
,然后添加和删除它(或者只显示和隐藏它):

//将其全部包装在一个中-为其指定一个类以确保
var redundantText='text';
$('#collapse_1')。on('show.bs.collapse',function(){
$(“.dropdown\u collapse\u 1”).addClass(“下拉指示器”);
$(“.dropdown\u collapse\u 1”).removeClass(“dropdown collapse”);
$(“#冗余#文本#包装”).append(redundantText);
});
$('#collapse_1')。on('hide.bs.collapse',function(){
$(“.dropdown\u collapse\u 1”).addClass(“dropdown collapse”);
$(“.dropdown\u collapse\u 1”).removeClass(“下拉指示器”);
//只需找到跨度并将其移除即可
$(“#冗余#文本_包装器>span.redundant”).remove();
});

好的,我现在尝试了另一种方法:

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();
}); 
var redundantText=“text”; $('#collapse_1')。on('show.bs.collapse',function(){ $(“.dropdown\u collapse\u 1”).addClass(“下拉指示器”); $(“.dropdown\u collapse\u 1”).removeClass(“dropdown collapse”); $(“#冗余#文本#包装”)。在(冗余文本)之后; }); $('#collapse_1')。on('hide.bs.collapse',function(){ $(“.dropdown\u collapse\u 1”).addClass(“dropdown collapse”); $(“.dropdown\u collapse\u 1”).removeClass(“下拉指示器”); $(“#redundantText_包装”).remove(); });

因此,基本上我使用'after'而不是'append',并将文本放入ID为的span中。现在我可以使用$('#ID')。remove();要在关闭选项卡时将其清除。

好的,我现在尝试了另一种方法:

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();
}); 
var redundantText=“text”; $('#collapse_1')。on('show.bs.collapse',function(){ $(“.dropdown\u collapse\u 1”).addClass(“下拉指示器”); $(“.dropdown\u collapse\u 1”).removeClass(“dropdown collapse”); $(“#冗余#文本#包装”)。在(冗余文本)之后; }); $('#collapse_1')。on('hide.bs.collapse',function(){ $(“.dropdown\u collapse\u 1”).addClass(“dropdown collapse”); $(“.dropdown\u collapse\u 1”).removeClass(“下拉指示器”); $(“#redundantText_包装”).remove(); });
因此,基本上我使用'after'而不是'append',并将文本放入ID为的span中。现在我可以使用$('#ID')。remove();关闭选项卡时将其清除。

更改
$(“#冗余_文本_包装”).text()。替换('redundantText',”)至:
$(“#冗余#文本_包装”).text().替换(冗余文本“”)@SuperDJ将其作为答案:@BhojendraNepal更改使用包含文本的变量。当前的方法是在text()中搜索不存在的
redundantText
。redundantText在上一步中被视为s字符串。您正在替换文本,但对结果更改不做任何操作
$