gpt4 book ai didi

javascript - 如何让 Javascript Word Counter 在 IE 中工作

转载 作者:行者123 更新时间:2023-11-28 07:56:13 26 4
gpt4 key购买 nike

我是 JavaScript 新手。我使用 JavaScript 在表单上设置了单词计数器。它在所有浏览器中都运行良好,除了 - 你猜对了 - IE。在 IE9 和 IE11 中,计数器都不可靠,整个字段可能会“卡住”,不允许您单击它并编辑任何内容。

为什么在 IE 中会崩溃?如果不能用简单的方法修复,是否有办法仅在浏览器不是 IE 时有条件地使用脚本?

以下是表单的演示:http://nzensandbox.wpmakeover.com/

代码:

$("#field_advstatement").on('keydown', function(e) {
var words = $.trim(this.value).length ? this.value.match(/\S+/g).length : 0;
if (words <= 50) {
$('#display_count2').text(words);
$('#word_left2').text(50-words)
}else{
if (e.which !== 8) e.preventDefault();
}
});

最佳答案

你可以试试这个

<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.5.js"></script>
<script>
function countChar(val) {
var len = val.value.length;
if (len >= 50) {
val.value = val.value.substring(0, 50);
} else {
$('#charNum').text(50 - len);
}
};
</script>
</head>

<body>
<textarea id="field" onkeyup="countChar(this)"></textarea>
<div id="charNum"></div>
</body>

</html>

关于javascript - 如何让 Javascript Word Counter 在 IE 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26050940/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com