【问题标题】:Jquery tag removal causing line breaks?Jquery标签删除导致换行符?
【发布时间】:2015-10-15 17:55:04
【问题描述】:

我正在使用脚本通过 keyup 事件突出显示与文本框中的条目匹配的容器中的文本。发生的情况是匹配的文本被包裹在一个跨度标签中,如下所示: (容器“我的文本搜索”中的文本,搜索词:“sear”)

My text <span class='highlight'>sear</span>
ch

当我尝试删除它以继续更长的搜索字符串(用户在文本框中输入另一个字符)时,我得到:

My text sear
ch

我对其进行了扫描,但它并未识别为换行符。有什么方法可以模拟“删除”字符以将该字符串重新组合在一起?否则,您可以看到为什么无法继续搜索完整的单词,因为标签删除会拆分单词。

去除方法

jQuery.fn.removeHighlight = function () {
$(this).find('.highlight').each(function () {
        var cont = $(this).contents();
       $(this).replaceWith(function() { return cont; });         
});
}

【问题讨论】:

  • 你想看什么(添加标签,删除标签,扫描换行符)?我试图简化问题,所以我可以得到一个概念性的答案......
  • 查看编辑...'删除方法'
  • 文本是否以其他方式标记?
  • 文本是
    标签的内部文本

标签: javascript jquery regex


【解决方案1】:

这是我想出的,我已经验证它有效...只是必须从不同的角度来解决它...

jQuery.fn.removeHighlight = function () {
$(this).find('.highlight').each(function () {
    var cont = $(this).contents();
    var tag = $(this).parent().prop('tagName');
    var tid = $(this).parent().attr('id');
    var testid = '#' + tid;
    var testTxt= '';

    $(this).replaceWith(function() { return cont; }); 


    testTxt = $(testid).text();
    testTxt = testTxt.replace(/(\r\n|\n|\r)/gm, "");
    $(testid).text(testTxt);


});

}

这将删除带有“highlight”类的 span 标签,并删除父级内部文本中标签删除后留下的任何后续换行符。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-10
    • 1970-01-01
    • 1970-01-01
    • 2011-05-13
    • 2014-12-22
    • 2021-08-12
    • 2012-01-05
    • 2011-10-27
    相关资源
    最近更新 更多