Maxlength在铬中不能正确计数,但在FF中效果不错 [英] Maxlength not counting properly in chrome but does well in FF
问题描述
我在textarea上使用maxlength,并使用JS来检查剩下的字符数。如果我尝试输入一个简单的字符串
即:
<123> 1234567890 p>
重复maxlength可以很好地工作,但是如果我尝试输入这样的字符串:
< blockquote>
欢迎!
1.这是一个测试
2这是一个测试
3这是一个测试
hahaahahahahahahaahahahahahahahhahaahahahahahahahahahahahaaaaaaaaaaaaaasddddddddddasdaasdasdasd
它在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文档,并且他们同意这个问题(以及大多数其他问题)。
由
空格计为字符,所以做特殊字符。通过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屋!