日期:2014-05-16 浏览次数:20349 次
<!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">
<!--<html>-->
<head>
<script type="text/javascript" src="jquery-1.9.1.js"></script>
</head>
<body>
<textarea style="overflow-y:auto; height: 50px;">sdfsadfsdfsadfddddddddddddddddddddddddddddsdfsadfsdfsadfddddddddddddddddddddddddddddsdfsadfsdfsadfddddddddddddddddddddddddddddsdfsadfsdfsadfddddddddddddddddddddddddddddsdfsadfsdfsadfddddddddddddddddddddddddddddsdfsadfsdfsadfddddddddddddddddddddddddddddsdfsadfsdfsadfddddddddddddddddddddddddddddsdfsadfsdfsadfddddddddddddddddddddddddddddsdfsadfsdfsadfddddddddddddddddddddddddddddsdfsadfsdfsadfddddddddddddddddddddddddddddsdfsadfsdfsadfddddddddddddddddddddddddddddsdfsadfsdfsadfddddddddddddddddddddddddddddsdfsadfsdfsadfddddddddddddddddddddddddddddsdfsadfsdfsadfddddddddddddddddddddddddddddsdfsadfsdfsadfddddddddddddddddddddddddddddsdfsadfsdfsadfddddddddddddddddddddddddddddsdfsadfsdfsadfddddddddddddddddddddddddddddsdfsadfsdfsadfddddddddddddddddddddddddddddsdfsadfsdfsadfddddddddddddddddddddddddddddsdfsadfsdfsadfddddddddddddddddddddddddddddsdfsadfsdfsadfddddddddddddddddddddddddddddsdfsadfsdfsadfdddddddddddddddddddddddddddd</textarea>
<button id="add">Add Textarea</button>
<button id="currentHeight">Current Height</button>
<button id="realHeight">Real Height</button>
</body>
<script>
$(function() {
$("textarea").each(function() {
$textarea = $(this);
var realHeight = $textarea.get(0).scrollHeight*2.7;
alert(realHeight);
if(realHeight > 100) {
$textarea.css("overflow-y", "auto");
$textarea.height(100);
}
});
/*$("textarea").keyup(function() {
if($textarea.height() > 100) {
$textarea.css("overflow-y", "auto");
$textarea.height(100);
}
});*/
$("#add").click(function() {
var html = "<textarea style='overflow-y:auto; height: 50px;'></textarea>";
$("body").append(html);
});
$("#currentHeight").click(function() {
alert($("textarea:first").height());
});
$("#realHeight").click(function() {
alert($("textarea:first").get(0).scrollHeight);
});
$(document).on("keyup", "textarea", function() {
var $textarea = $(this);
var realHeight = $textarea.get(0).scrollHeight;
var currentHeight = $textarea.height();