내가 시도하는 것은 텍스트를 입력하거나 텍스트를 붙여 넣을 때 Textarea 자동 확대를 만드는 것입니다. 그것은 IE7, IE8과 IE9, Firefox에서 잘 작동합니다. 그러나 OPERA, CHROME 및 SAFARI에서 Shift 키, Ctrl, 아래쪽 화살표, 위쪽 화살표, 왼쪽 화살표, 오른쪽 화살표, 홈, 끝, 삭제 등의 기능 키/추가 키를 클릭해도 자동으로 커집니다.Textarea 자동 확대가 Chrome에서 작동하지 않습니다.
jQuery 코드 :
<script type="text/javascript">
function txtareaAutoGrow(txtar, clkBtn){
// txtareaAutoGrow() start here
$("#"+txtar).height(18);
$("#"+txtar).keyup(function(){
if ($("#"+txtar).height() <= 18){
$(this).height(18);
}
else {
$(this).height($("#"+txtar).prop("scrollHeight"));
$("#"+txtar).bind('paste', function() {
setTimeout(function() {
$("#"+txtar).height($("#"+txtar).prop("scrollHeight"));
}, 100);
});
}
});
$("#"+clkBtn).click(function(){
if ($("#"+txtar).height() <= 18){
$("#"+txtar).height($("#"+txtar).prop("scrollHeight"));
}
else {
$("#"+txtar).height(18);
}
});
// txtareaAutoGrow() end here
};
</script>
HTML 코드 :
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Auto Resize TEXTAREA</title>
<style type="text/css">
textarea {
overflow:hidden;
resize: none;}
</style>
<script type="text/javascript" src="jquery-latest.js"></script>
<script type="text/javascript" src="autoresizeTextarea.js"></script>
<script type="text/javascript">
$(document).ready(function() {
txtareaAutoGrow("txtar1", "btnXpand1");
txtareaAutoGrow("txtar2", "btnXpand2");
});
</script>
</head>
<body>
<p>
<textarea cols="100" id="txtar1"></textarea>
<input type="button" id="btnXpand1" value="Expand/Collapse" />
<p> </p>
<textarea cols="100" id="txtar2"></textarea>
<input type="button" id="btnXpand2" value="Expand/Collapse" />
</p>
<p>
<!--<input type="button" id="Heght" value="Height" />
<input type="button" id="scrlHeight" value="Scroll Height" />
<input type="button" id="btnXpand" value="Expand/Collapse" />-->
</p>
</body>
</html>
미안 해요, 난 분명히 이전을 설명 일부러. 다시 한번 확인해주세요. –
나는 정말로 그것을 얻지 못합니까? 화살표 키를 누를 때 텍스트 영역의 높이가 증가한다는 것을 말하고 있습니까? 그리고 위의 게시 된 피들에서도이 작업을 수행합니까? – adeneo
코드가있는 HTML 페이지를 만들고 Chrome으로 엽니 다. 이제 텍스트를 추가해보십시오. Textarea는 모든 키 업에 대해 증가하고 있습니다. –