我在我的网站中为wysihtml5编辑器实现了一个字符计数器,但它在IE8中不起作用。我在以下代码行中收到错误,我用它来从WYSIWYG编辑器中获取当前字符数。
return document.getElementsByClassName('wysihtml5-sandbox')[0].contentWindow.document.body.innerText.length;
这适用于FF和Chrome,但IE8显示以下错误:
有没有办法让它在IE8中运行?
答案 0 :(得分:1)
$('.wysihtml5-sandbox')[0].contentWindow.document.body.innerText.length;
我试过这个并且它有效。我现在觉得很蠢。
答案 1 :(得分:0)
您可以自己编写,因为IE8不支持getElementByClassName。
编辑:更简单的方法是使用
document.querySelectorAll('.classname')
所以你想要:
return document.querySelectorAll('.wysihtml5-sandbox')[0].contentWindow.document.body.innerText.length;
这有用吗?
答案 2 :(得分: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));
}
我希望它可以帮到你!