【问题标题】:jquery text().replace('','') not workingjquery text().replace('','') 不工作
【发布时间】: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


【解决方案1】:

应该是:

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

或者如果你想替换所有出现的:

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

【讨论】:

    【解决方案2】:

    $( "#redundant_text_wrapper").text().replace('redundantText','') 只是获取文本值,替换它并且对结果不做任何事情,因此具有讽刺意味的是,这一行是多余的。

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

    如果你想操作一个元素的现有html,你应该使用html() which takes a function的重载(还有same available for text())。这接收元素的当前 HTML 并返回要设置的新 HTML:

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

    话虽如此,替换包装器的全部内容效率较低,并且可能会破坏其中元素上现有的事件处理程序之类的东西。我会说最好将多余的文本放入另一个元素中,例如 &lt;span&gt;,然后添加和删除它(或者只是显示和隐藏它):

    // wrap it all in a <span> - give it a class to be sure
    var redundantText = '<span class="redundant">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").append(redundantText);
    });
    
    $('#collapse_1').on('hide.bs.collapse', function() {
      $(".dropdown_collapse_1").addClass("dropdown-collapsed");
      $(".dropdown_collapse_1").removeClass("dropdown-indicator");
      // just find the span and remove it
      $("#redundant_text_wrapper > span.redundant").remove();
    });
    

    【讨论】:

    • 你是对的,虽然这只是我帖子中的一个错字。我在变量周围没有引号。
    【解决方案3】:

    改变

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

    收件人:

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

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

    或者

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

    这样它会先获取文本而不是替换它并设置它。

    【讨论】:

    • 那是我帖子中的一个错误,我有那个。无论如何都不起作用:(
    • 您是否也尝试过使用html() 而不是text()。因为html()也会得到a href的html标签
    【解决方案4】:

    好的,我现在尝试了不同的方法:

    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 的 span 中。现在我可以使用 $('#ID').remove();在关闭标签时摆脱它。

    【讨论】:

    • 这没关系,只是您现在的 html 中暂时有重复的 ID,这是无效的
    • 嗯,我看不出哪个 ID 会重复。当我打开下一个选项卡时,订单会自动关闭。这样 hide.bs.collapse 函数会触发并从另一个选项卡中删除 #redundantText_wrapper。对于所有其他选项卡,都有 ID,例如 #collapse_2、#collapse_3
    猜你喜欢
    • 2013-06-07
    • 2017-11-13
    • 1970-01-01
    • 1970-01-01
    • 2012-04-27
    • 1970-01-01
    • 2013-10-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多