2015-10-15 74 views
0

我正在使用腳本來突出顯示通過keyup事件匹配文本框中條目的容器中的文本。什麼情況是,這是符合被包裹在像這樣的跨度標籤的文字: (集裝箱「我的文本搜索」文本,檢索詞:「烤焦」)jquery標籤刪除導致換行符?

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; });   
}); 
} 
+0

你想看到什麼(添加標籤,刪除標籤,掃描換行符)?我試圖簡化問題,所以我可以得到一個概念性的答案... – Andrew

+1

請參閱編輯...'刪除方法' – Andrew

+0

是否以其他方式標記文本? – saluce

回答

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標記,並刪除父標記內部文本中標記刪除後留下的任何後續換行符。