“最大长度"与“最大长度"之间的区别在于& amp;& quot;尺寸& quot;HTML中的属性? [英] Difference between "maxlength" & "size" attribute in html?

查看:59
本文介绍了“最大长度"与“最大长度"之间的区别在于& amp;& quot;尺寸& quot;HTML中的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于 maxlength size 属性之间的区别,我有点困惑.

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

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

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

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).

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

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.

size 属性仅用于视觉渲染.它以平均"字符的形式建议字段的可见宽度.这个模糊的概念尚未在规范中阐明,并且浏览器会不一致地实现它.使用等宽字体时,效果最佳.此属性不限制输入的字符数,但会影响可用性:例如,很难在一个字段中输入30个字符长的字符串,而该字段一次只能看到10个字符.字段的宽度也是向用户发出的信号:它表示输入的预期最大宽度.

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.

这两个属性通常都具有相同的值,通常是适合的.例如,如果该字段用于5位邮政编码,则 size = 5 maxlength = 5 是合适的,尤其是如果您还设置了 font-family:monospace ,则实际宽度或多或少恰好是五位数.

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.

但是,值可能会有所不同.例如,在邮寄地址中询问行时,您可以设置 size = 30 ,因为通常这对于一行来说就足够了,但是如果符合,则设置 maxlength = 80 不受数据库或数据处理设置的限制,并且没有特别的理由不允许这么长的行.

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 allow such long lines.

size 属性原则上可以用 CSS 代替,因为它仅处理视觉渲染.但是,宽度通常最好用字符设置, CSS 中没有统一支持的字符平均宽度单位.新的 ch 单元非常接近,但功能并不完全相同,旧浏览器也不支持.

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.

这篇关于“最大长度"与“最大长度"之间的区别在于&amp; amp;&amp; quot;尺寸&amp; quot;HTML中的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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