gpt4 book ai didi

javascript - JQuery/JavaScript 字符限制计数器和文本突出显示

转载 作者:行者123 更新时间:2023-11-28 00:18:23 25 4
gpt4 key购买 nike

我正在尝试实现这个功能...我有一个最大字符数限制为 600 个的文本区域(不包括数字和符号)和一个计数器(#chars)。当超过字符限制时,发送按钮(.send-message)将被禁用,字 rune 本计数器(#chars)将变为红色。

问题是我希望过多的字符(仅超过 600 个)在键盘上也变成红色。

$(document).ready(function(){
var char = 600; // Max character limit
$("#chars").html(char);
$("#editor").keyup(function () {
if ($("#editor").text().replace(/[^A-Z]/gi,"").length > char){
$('.send-message').removeClass('btn-default');
$('.send-message').addClass('disabled btn-danger');
}else if($("#editor").text().replace(/[^A-Z]/gi,"").length < 1){
$('.send-message').removeClass('disabled');
$('.send-message').addClass('btn-default');
}
else if(char <= 600){
$('.send-message').removeClass('disabled btn-danger');
$('.send-message').addClass('btn-default');
$('.send-message').attr('type', 'submit');
}
var rest = char - $(this).text().replace(/[^A-Z]/gi,"").length;
$("#chars").html(rest);

if (rest <= 100) {
$("#chars").css("color", "#f00");
}
else {
$("#chars").css("color", "#111111");
}

$("#excessChars") = $("editor").text().replace(/[^A-Z]/gi,"").length > char);
if (rest <= 0) {
$(rest).css("color", "#f00").text();
}
});
});

最佳答案

html

    <!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Accordion - Default functionality</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery-easing/1.3/jquery.easing.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/js/bootstrap.min.js"></script>
<link href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">

</head>

<style>
textarea {position: absolute; height: 100px; width: 300px; top: 100px; left: 30px;}
#test1 {background: rgba(0,0,0,0); color: red;}
#test2 {background: rgba(0,0,0,0); color: #000;}
</style>
<body>
<textarea id="test1"></textarea>
<textarea id="test2"></textarea>

<script>
$(document).ready(function(){
console.log('here');
$('#test2').on({
focus: function() {
console.log('here');
if (this.value.length >= 20) {
$('#test1').focus();
console.log(this.value.length);
}
},
keyup: function() {
if (this.value.length >= 20) $('#test1').focus();
$('#test1').val(this.value);
}
});
});

</script>
</body>
</html>

关于javascript - JQuery/JavaScript 字符限制计数器和文本突出显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30264840/

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