“最大长度”之间的差异&安培; "大小"属性在HTML? [英] Difference between "maxlength" & "size" attribute in html?

查看:111
本文介绍了“最大长度”之间的差异&安培; "大小"属性在HTML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 maxlength size 属性之间的区别有点困惑。

 < input type =textname =telephonemaxlength =30size =34> 

我知道 maxlength 用于控制输入数据。那么,一次使用这两个属性的原因是什么?

maxlength (不是 max-长度)属性以字符或更准确地说以代码单位指定输入字符串的最大长度。预计浏览器会强制执行此操作,拒绝接受更多字符。然而,这并不意味着作为一种安全措施,因为它可以轻松地被覆盖。相反,它告诉用户在处理数据时不会接受更多的字符。当你必须设定一个上限时,这是很有用的,例如你的数据库只能存储一些固定数量的字符以及某些信息,当长度存在逻辑限制时(例如,如果数据是美国状态的双字母代码,它的逻辑上限为2 )。



maxlength 属性因此是合乎逻辑的,即使在非可视化用户界面。这并不意味着以任何方式影响输入字段的视觉外观。



size 属性,而仅用于可视化呈现。就平均字符而言,它表明字段的可见宽度。这个模糊的概念在规范中尚未明确,浏览器也不一致地实现。它使用等宽字体时效果最好。此属性不限制输入的字符数量,但会影响可用性:在字段中输入长度为30个字符的字符串很困难,因为该字段一次只能看到10个字符。字段宽度也是向用户发出的信号:它表示输入的预期最大宽度。



通常适合使用这两个属性,通常与相同的价值。例如,如果该字段用于5位邮政编码,则 size = 5 maxlength = 5 是合适的,特别是如果您还设置了 font- family:monospace ,所以实际的宽度大致是五位数。



但是,这些值可能会有所不同。例如,当在邮政地址中询问一条线时,可以设置 size = 30 ,因为这对于一条线通常是足够的,但 maxlength = 80 ,如果这对应于您的数据库或数据处理所设置的限制,并且您没有特别的理由不允许这样长的行。



size 属性原则上可以用CSS代替,因为它仅处理可视化渲染。但是,宽度通常是以字符为单位的最佳设置,并且CSS中字符的平均宽度没有普遍支持的单位;新的 ch 单元接近,但不完全相同,旧版浏览器不支持。


I am little bit confused about the difference between the maxlength and the size attribute.

<input type="text" name="telephone" maxlength="30" size="34">

I know that maxlength is used to control the input data. So what is the reason for using both attributes at once?

解决方案

The maxlength (not max-length) attribute specifies the maximum length of the input string in characters or, more exactly, in code units. The browser is expected to enforce this, by refusing to accept more characters. However, this is not meant to act as a security measure, since it can be overridden trivially. Rather, it tells the user that no more characters will be accepted in processing the data. This is useful when you have to set an upper limit, e.g. your database can store only a fixed number of characters fpr some information, and also when there is a logical limit on the length (e.g., if the data is a two-letter code for a US state, it has the logical upper limit of 2).

The maxlength attribute is thus logical, and it is expected to work even in non-visual user interface. It is not meant to affect the visual appearance of the input field in any way.

The size attribute, in contrast, is for visual rendering only. It suggests a visible width for the field, in terms of "average" characters. This vague concept has not been clarified in specifications, and browsers implement it inconsistently. It works best when a monospace font is used. This attribute does not limit the amount of characters entered, but it affects usability: it is difficult to enter, say, a 30 characters long string in a field that lets you see only 10 characters at a time. The width of a field is also a signal to the user: it indicates the expected maximum width of the input.

It is often suitable to use both attributes, often with the same value. For example, if the field is for a 5-digit postal code, size=5 maxlength=5 is suitable, especially if you also set font-family: monospace, so that the actual width is more or less exactly five digits.

However, the values may differ. E.g., when asking for a line in a postal address, you might set size=30, since this is normally sufficient for a line, but maxlength=80, if this corresponds to the limitations set by your database or data processing and you have no particular reason not to allo such long lines.

The size attribute can in principle be replaced by CSS, since it deals with visual rendering only. However, the width is usually best set in characters, and there is no universally supported unit for the average width of a character in CSS; the new ch unit comes close, but isn’t quite the same and isn’t supported by old browsers.

这篇关于“最大长度”之间的差异&安培; &QUOT;大小&QUOT;属性在HTML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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