This article describes the method of JS to implement adaptive height form text boxes. Share it for your reference. The specific implementation method is as follows:
Copy the code as follows:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>JS implements adaptive height form text box</title>
<style type="text/css">
#shadow, #text { font: 12px/16px Arial; width: 200px; overflow: hidden; height: 16px; }
#shadow { position: absolute; border-width: 0px; padding: 0px; visibility: hidden; }
#text { resize: none; }
</style>
<script type="text/javascript">
window.onload = function () {
var text = document.getElementById("text"); //The text box seen by the user
var shadow = document.getElementById("shadow"); //Hidden text box
text.oninput = //Non-IE
text.onpropertychange = //IE's
onchange;
function onchange() {
shadow.value = text.value;
setHeight();
setTimeout(setHeight, 0); //For the delay of IE 6/7/8, otherwise there will be a character entry and exit
function setHeight() { text.style.height = shadow.scrollHeight + "px"; }
}
};
</script>
</head>
<body>
<textarea id="text"></textarea>
<textarea id="shadow"></textarea>
</body>
</html>
I hope this article will be helpful to everyone's JavaScript programming.