我正在使用CKEditor並創建了一個Post
按鈕來發布用戶在編輯器中鍵入的內容。我希望Post
按鈕可以根據是否在編輯器中輸入實際文本來禁用或啓用。但是,如果用戶只需點擊表單元素(B
,I
等)而不鍵入,則DOM填充如下空標籤:<strike><b></b><i></i></strike>
。這使我的解析器來解釋文本區域具有文本,當我做這個檢查:從jQuery變量中過濾文本
this.textData = $('.noteEditor1').ckeditorGet().getSnapshot()
.replace(/<br\s*[\/]?>/gi, "\n")
.replace(/ /g, " ");
//if there is no style on the text, $(this.textData).text() will be an empty string, so make sure it contains brackets and isn't empty
// before using it
if(this.textData.indexOf('<') !== -1 && this.textData.indexOf('>') !== -1 && $(this.textData).text() !== "") {
this.textData = $(this.textData).text();
}
//have to remove zero width spaces characters so they aren't counted in the text length
var text = this.textData.replace(/[\n?\r?\t?]/g, "")
.replace(/[\u200B-\u200D\uFEFF]/g, '');
if(text.length > 0) {
$('.footer-btn').removeAttr('disabled');
} else {
$('.footer-btn').attr('disabled', 'disabled');
}
我發現這個jQuery的過濾器上this question我認爲會做我想做的,當我把多個選擇它。
$('b, i, u, strike, sub, sup').filter(function(){
return $(this).text().trim().length===0;
}).remove();
我該如何讓這個過濾器在我的變量this.textData
上運行?
EDIT!重要
我加了函數的其餘部分,因爲它看起來像這個問題來自於if
聲明。我必須這樣做,因爲如果我不這樣做,那麼沒有任何樣式的文本會被剝離。然而,有了它,我結束了而不是使用$(this.textData).text()
當我只有空標籤,這意味着標籤附近,因此我必須過濾它們。如何確保我不清除未格式化的文本,但仍然清除空格式標記?
我試着用上面這樣的過濾器:
$(text).find('b, i, u, strike, sub, sup').filter(function(){
return $(this).text().trim().length===0;
}).remove();
但是,這並不工作。
你能提供'this.textData'價值的例子嗎? – DanC 2013-05-03 19:43:48
什麼是textData。它有什麼? – PSL 2013-05-03 19:43:58
根據你提供的這個「應該」正在工作的最小例子。 http://jsfiddle.net/ANtKJ/ – DanC 2013-05-03 19:45:26