我有这个jQuery代码:
var char = 60;
$("#counter").append("You have <strong>" + char + "</strong> char.");
$("#StatusEntry").keyup(function () {
if ($(this).val().length > char) {
$(this).val($(this).val().substr(0, char));
}
var rest = char - $(this).val().length;
$("#counter").html("You have <strong>" + rest + "</strong> char.");
if (rest <= 10) {
$("#counter").css("color", "#ff7777");
}
else {
$("#counter").css("color", "#111111");
}
});
Run Code Online (Sandbox Code Playgroud)
它工作正常!但如果相反,val()我有text()它不起作用.
问题是,在可用的char结束时,它开始从一开始就替换文本......使用val是完美的.
为什么我需要它在文本上?因为我正在使用一个所见即所得的插件,它将我的textarea转换为div.
我正在尝试.stopPropagation但它不起作用..尝试返回false而没有...
希望对你有所帮助!