如何在书写时自动扩展文本区域?
这是我的代码:
<script type="text/javascript">
function clearContents(element) {
element.value = '';
}
function updateContents(element) {
element.value = "What's on your mind?";
}
</script>
<h1>New status</h1>
<form action="index.php" method="post" name="new_message">
<textarea id="message" onFocus="clearContents(this);" onKeyPress="catchEnter(this);">What's on your mind?</textarea>
<input type="button" value="Post">
</form>
最佳答案
我一直在使用 elastic textbox from Unwrongest这是一个很棒的插件。
只包含插件,然后用这个调用插件:
$('#TextAreaId').elastic();
就是这样。
在此处查看演示:http://jsfiddle.net/janjarfalk/r3Ekw/
如何通过 3 个步骤实现此目的:
第 1 步:将此添加到您的 <head>
在你的 html 中:
http://jquery-elastic.googlecode.com/svn/trunk/jquery.elastic.source.js
第 2 步:将此添加到您的 html 文件中,就在 </body>
之前标签:
$('textarea').elastic();
第 3 步:只有在第 2 步之后不起作用时才使用此步骤。在第 1 步的代码上方添加下一行:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript"></script>
你完成了。如果您已经有 jQuery,请跳过第 3 步。
关于javascript - 如何在我们写的时候自动扩展文本区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7077642/