0

我正在使用 CKEditor 并创建了一个Post按钮来发布用户在编辑器中键入的内容。我希望Post根据是否在编辑器中输入了实际文本来禁用或启用该按钮。但是,如果用户只是点击表单元素(BI等)而没有输入,则 DOM 会被空标签填满,如下所示<strike><b></b><i></i></strike>:这会导致我的解析器在我执行此检查时将 textarea 解释为具有文本:

    this.textData = $('.noteEditor1').ckeditorGet().getSnapshot()
                                                .replace(/<br\s*[\/]?>/gi, "\n")
                                                .replace(/&nbsp;/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 过滤器,我认为当我在其中放置多个选择器时它会做我想做的事情。

$('b, i, u, strike, sub, sup').filter(function(){
    return $(this).text().trim().length===0;
}).remove();

如何让这个过滤器在我的变量上运行this.textData

编辑!重要

我添加了函数的其余部分,因为看起来问题来自该if语句。我必须进行检查,因为如果我不这样做,那么没有任何样式的文本就会被删除。但是,当我只有空标签时,我最终不会使用它$(this.textData).text(),这意味着标签会一直存在,因此我必须过滤它们。如何确保不清除未格式化的文本但仍清除空的格式标记?

我尝试像这样使用上面的过滤器:

$(text).find('b, i, u, strike, sub, sup').filter(function(){
    return $(this).text().trim().length===0;
}).remove();

但这不起作用。

4

1 回答 1

1

好的,最后一枪

this.textData = $('.noteEditor1').ckeditorGet().getSnapshot();
var text =  $.trim($(this.textData).text()); 

if(text.length > 0) {
    $('.footer-btn').removeAttr('disabled');
} else {
    $('.footer-btn').attr('disabled', 'disabled');
}
于 2013-05-03T20:06:56.930 回答