利用 JavaScript,當我在 HTML 文字輸入欄位中輸入時讓欄位自動增長?
使用 來實現這一點。因為它需要你輸入內容,請確保將 設定為 -
contenteditable="true"
例項
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initialscale=1.0">
<title>Document</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/fontawesome/4.7.0/css/font-awesome.min.css">
<style>
span{
border: solid 2px skyblue;
}
div{
max-width: 500px;
}
</style>
</head>
<body>
<div>
<span contenteditable="true">John Smith</span>
</div>
<script>
</script>
</body>
</html>要執行上面的程式,請儲存檔名“anyName.html(index.html)”,然後右鍵單擊該檔案。在 VS Code 編輯器中選擇“使用 Live Server 開啟”選項。
輸出
這會生成如下輸出 -

廣告
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP