Maxlength在铬中不能正确计数,但在FF中效果不错 [英] Maxlength not counting properly in chrome but does well in FF

查看:75
本文介绍了Maxlength在铬中不能正确计数,但在FF中效果不错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在textarea上使用maxlength,并使用JS来检查剩下的字符数。如果我尝试输入一个简单的字符串



即:


<123> 1234567890
p>

重复maxlength可以很好地工作,但是如果我尝试输入这样的字符串:

< blockquote>

 欢迎! 

1.这是一个测试
2这是一个测试
3这是一个测试

haha​​ahahahahahahaahahahahahahahhahaahahahahahahahahahahahaaaaaaaaaaaaaasddddddddddasdaasdasdasd


它在6个字符左右停止,但在FF上完美运行。



我试图做一些关于maxlength如何工作的研究,但它没有成功,所以我想知道为什么maxlength在chrome上不能很好地工作。可能是因为输入了空格和特殊字符?

谢谢

解决方案

这个问题可以用简单的代码来演示,如

 < textarea maxlength = 5 rows = 2 cols = 10> < / textarea的> 

在Firefox上,您可以键入12,按回车键,然后键入34,然后再输入更多。在Chrome上,如果您尝试这样做,则无法在3之后再输入。



Chrome似乎可以正常工作,而Firefox不会。 textarea maxlength 属性是HTML5的新颖之处,因此正确性的最佳定义是最新的W3C HTML5草稿或最新的WHATWG HTML文档,并且他们同意这个问题(以及大多数其他问题)。



textarea 的W3C HTML5定义换行中的换行符被浏览器规范化为CR LF(回车符,换行符,即两个控制字符)。因此,如果您输入12,按enterm键并输入3,则您输入了五个字符,因为换行符计为两个字符。



空格计为字符,所以做特殊字符。通过W3C HTML5草案,一些非常特殊的字符,即基本多语言平面(BMP)以外的字符是非常特殊的:它们将每个字符计数为两个字符,因为长度以16位代码单元计数。但大多数人从未使用非BMP字符。


I am using maxlength at a textarea, and using a JS to check the number of characters left. if i try to input a simple string

i.e:

1234567890

repeatedly the maxlength works perfectly but if i try to input a string like this:

Welcome!

1. this is a test
2. this is a test
3. this is a test

hahaahahahahahahaahahahahahahahhahaahahahahahahahahahahahaaaaaaaaaaaaaasddddddddddasdaasdasdasd

it stops on 6 characters left, but on FF it works perfectly.

I was trying to do some research on how maxlength works but it was to no success, so i was wondering why is maxlength not working perfectly on chrome. could it be because of the spaces and special characters inputted?

thank you

解决方案

The issue can be demonstrated using simple code like

<textarea maxlength=5 rows=2 cols=10></textarea>

On Firefox, you can then e.g. type "12", press enter, and type "34", and then you can’t type more. On Chrome, if you try to do the same, you cannot type any more after the "3".

It seems that Chrome works correctly, Firefox doesn’t. The maxlength attribute for textarea is an HTML5 novelty, so the best definition for correctness is either the newest W3C HTML5 draft or the newest WHATWG HTML document, and they agree on this issue (and most other issues too).

By the W3C HTML5 definition for textarea, a line break in the value is canonicalized by the browser to CR LF (carriage return, linefeed, i.e. two control characters). So if you type "12", press enterm, and type "3", you have entered five characters, since the line break counts as two characters.

Spaces count as characters, and so do special characters. By the W3C HTML5 draft, some very special characters, namely characters outside Basic Multilingual Plane (BMP), are relly special: they count each as two characters, since the length is counted in 16-bit code units. But most people never used non-BMP characters.

这篇关于Maxlength在铬中不能正确计数,但在FF中效果不错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆