1

我在我的站点中为 wysihtml5 编辑器实现了一个字符计数器,但它在 IE8 中不起作用。我在以下代码行中收到错误,我使用该代码从 WYSIWYG 编辑器获取当前字符数。

return document.getElementsByClassName('wysihtml5-sandbox')[0].contentWindow.document.body.innerText.length;

这适用于 FF 和 Chrome,但 IE8 显示以下错误:

有没有办法让它在 IE8 中工作?

4

3 回答 3

1
$('.wysihtml5-sandbox')[0].contentWindow.document.body.innerText.length;

我试过这个并且它有效。我现在觉得很傻。

于 2013-11-19T18:58:33.287 回答
0

您可以自己编写,因为 IE8 不支持 getElementByClassName。

编辑:更简单的方法是使用

document.querySelectorAll('.classname')

所以你要:

return document.querySelectorAll('.wysihtml5-sandbox')[0].contentWindow.document.body.innerText.length;

那样有用吗?

于 2013-11-19T18:47:43.233 回答
0

我用过这段代码。

此行必须在您创建 WYSIHTML5 对象之后:

$('.wysihtml5-sandbox').contents().find('body').on("keyup",countChars);

函数“countChars”定义为:

function countChars() {
    var text = $('.wysihtml5-sandbox').contents().find('body').html().replace(' ',' '); //Remove space tags
    var limit = 50; //Max text lenght

    if(text.length > limit) {
       //Your action when it's invalid
    } else {
       //Your action when it's valid
    }

    //In my case this is a span where the number is show
    var remains = limit - text.length;
    $('#char-counter').html(String(remains)); 
}

我希望它可以帮助你!

于 2015-06-18T23:14:59.097 回答