我在我的html页面中使用输入类型文本。现在,我想根据输入文本字段的宽度限制字符。我的输入字段宽度为400 95,我希望将字符限制到我字段宽度的95%。
使用maxlength属性限制最大字符不适合我的工作流。
有人能给我个建议吗。
发布于 2015-02-09 13:05:38
$(function(){
var noc= .95*parseInt($("#txt").width())/parseInt($("#txt").css("font-size"));
$("#txt").attr("maxlength",parseInt(noc));
$("#maxlengthfound").text(parseInt(noc));
})
#txt{width:200px;font-size:10px}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="text" id='txt'/>
<label id='maxlengthfound'></label>
https://stackoverflow.com/questions/28410211
复制相似问题