我可以在CSS中将maxlength属性替换为某些东西吗?
<input type='text' id="phone_extension" maxlength="4" />
Answers:
没有。
maxlength用于行为。
CSS是用于样式的。
这就是为什么。
不是使用CSS,而是可以使用JavaScript模拟和扩展/自定义所需的行为。
正如其他人回答的那样,当前没有将maxlength直接添加到CSS类的方法。
但是,这种创造性的解决方案可以实现您想要的。
我在名为maxLengths.js的文件中有jQuery,该文件在站点中引用(ASP的site.master)
运行该代码段以查看其运行情况,效果很好。
jQuery,CSS,HTML:
$(function () {
$(".maxLenAddress1").keypress(function (event) {
if ($(this).val().length == 5) { /* obv 5 is too small for an address field, just want to use as an example though */
return false;
} else {
return true;
}
});
});
.maxLenAddress1{} /* this is here mostly for intellisense usage, but can be altered if you like */
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<input type="text" class="maxLenAddress1" />
使用它的好处是:如果确定需要将这种类型的字段的最大长度或整个应用程序中的最大长度推出,则可以在一个位置进行更改。对于客户代码,全名字段,电子邮件字段以及整个应用程序中常见的任何字段,该字段长度都很方便。