获取没有标签的CKEditor内容的第一个300个字符



我正在使用ckeditor,在用户进入描述中后,我想将前300个字符放在摘要文本框中。我正在使用下面的代码来使内容从描述CKEditor框移动后移动。当我提醒值时,它将包括<p>标签。我无法剥离用户打算使用它们的字符串。

CKEDITOR.replace( 'property_description' );
CKEDITOR.instances['property_description'].on('blur', function() {
    var value = CKEDITOR.instances['property_description'].getData();
    alert(value);
});

使用普通JavaScript:

CKEDITOR.replace('property_description');
CKEDITOR.instances['property_description'].on('blur', function() {
    var html = CKEDITOR.instances['property_description'].getData();
    var tmp = document.createElement('div');
    // Strip HTML
    tmp.innerHTML = html;
    var value = tmp.textContent || tmp.innerText;
    // alert truncated value
    alert(value.substring(0, 300);
});

使用jQuery:

CKEDITOR.replace('property_description');
CKEDITOR.instances['property_description'].on('blur', function() {
    var html = CKEDITOR.instances['property_description'].getData();
    var value = $('<div/>', { html: html }).text();
    // alert truncated value
    alert(value.substring(0, 300);
});
CKEDITOR.replace( 'property_description' );
CKEDITOR.instances['property_description'].on('blur', function() {
    var value = CKEDITOR.instances['property_description'].getData();
    var chars = (((value.replace(/(<([^>]+)>)/ig, "")).replaceHtmlEntites()).replace(/ /g, '')).substring(0, 300);
        alert(chars)
});

String.prototype.replaceHtmlEntites = function () {
    var s = this;
    var translate_re = /&(nbsp|amp|quot|lt|gt);/g;
    var translate = {"nbsp": "", "t": "", "amp": "&", "quot": """, "lt": "<", "gt": ">"};
    return ( s.replace(translate_re, function (match, entity) {
        return translate[entity];
    }) );
};

尝试使用jQuery

最新更新