Html 我可以在 css 中指定 maxlength 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1363030/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I specify maxlength in css?
提问by Brian Ramsay
Can I replace the maxlength attribute with something in CSS?
我可以用 CSS 中的东西替换 maxlength 属性吗?
<input type='text' id="phone_extension" maxlength="4" />
采纳答案by edeverett
No. This needs to be done in the HTML. You could set the value with Javascript if you need to though.
不可以。这需要在 HTML 中完成。如果需要,您可以使用 Javascript 设置该值。
回答by
No.
不。
maxlength is for behavior.
maxlength 用于行为。
CSS is for styling.
CSS 用于样式化。
That is why.
这就是为什么。
回答by macio.Jun
You can use jQuery like:
你可以像这样使用 jQuery:
$("input").attr("maxlength", 4)
Here is a demo: http://jsfiddle.net/TmsXG/13/
这是一个演示:http: //jsfiddle.net/TmsXG/13/
回答by Charlie
I don't think you can, and CSS is supposed to describe how the page looks not what it does, so even if you could, it's not really how you should be using it.
我认为你不能,而且 CSS 应该描述页面的外观而不是它的功能,所以即使你可以,它也不是你应该如何使用它。
Perhaps you should think about using JQuery to apply common functionality to your form components?
也许您应该考虑使用 JQuery 将通用功能应用于表单组件?
回答by JMP
Not with CSS, no.
不使用 CSS,不。
回答by code_burgar
Not with CSS, but you can emulate and extend / customize the desired behavior with JavaScript.
不是使用 CSS,但您可以使用 JavaScript 模拟和扩展/自定义所需的行为。
回答by Taylor Brown
As others have answered, there is no current way to add maxlength directly to a CSS class.
正如其他人所回答的那样,目前没有将 maxlength 直接添加到 CSS 类的方法。
However, this creative solution can achieve what you are looking for.
但是,这种创造性的解决方案可以实现您的需求。
I have the jQuery in a file named maxLengths.js which I reference in site (site.master for ASP)
我在名为 maxLengths.js 的文件中有 jQuery,我在站点中引用了该文件(ASP 的 site.master)
run the snippet to see it in action, works well.
运行代码片段以查看它的运行情况,效果很好。
jquery, css, html:
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" />
The advantage of using this: if it is decided the max length for this type of field needs to be pushed out or in across your entire application you can change it in one spot. Comes in handy for field lengths for things like customer codes, full name fields, email fields, any field common across your application.
使用它的好处是:如果决定需要在整个应用程序中推出或推出此类字段的最大长度,您可以在一个地方更改它。对于诸如客户代码、全名字段、电子邮件字段、应用程序中常见的任何字段等字段长度来说非常有用。
回答by Rohit
Use $("input").attr("maxlength", 4)
if you're using jQuery version < 1.6
and $("input").prop("maxLength", 4)
if you are using jQuery version 1.6+.
使用$("input").attr("maxlength", 4)
,如果你使用jQuery版本<1.6,$("input").prop("maxLength", 4)
如果你正在使用jQuery版本1.6或更高版本。