[英]How do I stop expanding the width of the input box after it reaches a certain distance (measured in pixels)?
我對這個特定代碼有問題:
<html>
<head>
<script type="text/javascript">
function Expand(obj){
if (!obj.savesize) obj.savesize=obj.size;
obj.size=Math.max(obj.savesize,obj.value.length);
}
</script>
</head>
<body>
<form>
<input type="text" size="5" style="font-family:Courier;" onkeyup="Expand(this);">
</form>
</body>
</html>
問題是,它工作正常,但輸入框的寬度不斷增加和增加。 我希望擴展在達到某個限制后停止。 我怎么做?
像這樣使用Math.min
(假設您的最大尺寸為 30):
<html>
<head>
<script type="text/javascript">
function Expand(obj) {
if (!obj.savesize) obj.savesize = obj.size;
obj.size = Math.min(30, Math.max(obj.savesize, obj.value.length));
}
</script>
</head>
<body>
<form>
<input type="text" size="5" style="font-family:Courier;" onkeydown="Expand(this);">
</form>
</body>
</html>
無論如何,我建議您使用 CSS 設置輸入樣式並從那里設置寬度。 如果你不知道怎么做,這可能是一個很好的起點: https : //www.w3schools.com/css/css_form.asp
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.