我創建了一個輸入(輸入文本)框並使其自動調整大小非常簡單。不過,也有,我似乎無法修復一些小問題:自動調整大小的文本輸入框html
- 當我開始輸入框縮小一點點
- 當我按空格鍵(或定向箭頭),盒子首先擴展,然後在繼續打字時縮小。
這裏是我的代碼:
function Expander() {
\t
this.start = function() {
\t \t \t
$("#inputbox").keydown(function(e) {
\t \t \t
this.style.width = 0;
var newWidth = this.scrollWidth + 10;
\t \t \t
if(this.scrollWidth >= this.clientWidth)
newWidth += 10;
\t \t \t \t
this.style.width = newWidth + 'px';
\t \t \t
});
\t \t
}
\t
}
$(function() {
\t window.app = new Expander();
\t window.app.start();
});
input {
\t margin-left:3em;
\t min-width:100px;
\t max-width:600px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<meta charset="UTF-8">
<body>
<div id="wrap">
\t <input type="text" id="inputbox" name="input" placeholder="I want this to resize smoothly." width="100px"/>
</div>
<div id="counter"></div>
</body>
只是一個觀察,思考粘貼文本或按某個鍵並保持。也許你應該看看這個答案的插件:相反,請參閱http://stackoverflow.com/questions/931207/is-there-a-jquery-autogrow-plugin-for-text-fields。 – Pricey
謝謝,我還沒有完成我的代碼,只是想知道爲什麼它有問題。我永遠不會學習語言,如果我只是從別人的代碼:) – rockerist
你不需要只是採取代碼,你可以分析他們的代碼在做什麼,所以你可以編寫自己的插件,我發現時,非常有用的學習。 – Pricey