我想知道如何在编辑时向文本区域添加新行,即当字母数超过文本区域的列数时自动添加新行。
I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....
我想知道如何在编辑时向文本区域添加新行,即当字母数超过文本区域的列数时自动添加新行。
一些实现这一点的基本代码,在这个jsfiddle中进行了测试(http://jsfiddle.net/NZbZn/)。显然,这将需要工作,但显示了基本概念。
jQuery(document).ready(function(){
//after typing occurs
jQuery( textarea#expander ).keyup(function(){
//figure out how many chars
var length = jQuery(this).val().length;
//if more than 10
if(length > 10){
//if the modulus 10 of the length is zero, there are a multiple of 10 chars, so we need to extend by a row
//(NOTE the obvious problem - this will expand while deleting chars too!)
if((length % 10) == 0){
//figure out the number of rows, increment by one, and reset rows attrib
var rows = jQuery(this).attr( rows );
rows++;
jQuery(this).attr( rows , rows);
}
}
});
});
不过,还是要先考虑一下UI的含义。
$( textarea ).keyup(function (e) {
var rows = $(this).val().split("
");
$(this).prop( rows , rows.length);
});
您可以使用jQuery AutoResize插件:http://james.padolsey.com/javascript/jquery-plugin-autoresize/
你可以这样做:
<textarea id="ta" onkeyup="checkScroll(this)" style="height:4em; overflow:auto">
asdf
</textarea>
<script>
function checkScroll(obj) {
if(obj.clientHeight < obj.scrollHeight) {
obj.style.height = (parseInt(obj.style.height)+1) + em ;
}
}
</script>
如果你愿意的话,你可以在“如果”条件下增加一个高度限制。
(不用担心让这件事变得不引人注目——只是把重点传达出来。)
试试这个
var txt = $("textarea#idhere");
txt.val( txt.val() + "
Something here
Again");
这是一个普通的JS单行,它自动将文本区域的行数设置为包含的行数:
elt = document.getElementsByTagName( textarea )[0]
elt.addEventListener( keyup ,()=>elt.setAttribute( rows ,elt.value.split(
).length))
<textarea rows= 1 >Type some lines!</textarea>
I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....
I have a div <div id="masterdiv"> which has several child <div>s. Example: <div id="masterdiv"> <div id="childdiv1" /> <div id="childdiv2" /> <div id="...
I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...
<form><input type="file" name="first" onchange="jsFunction(2);"> <input type="file" name="second" onchange="jsFunction(3);"</form> Possible to pass just numbers to the js ...
So I ve got a menu with a hover/selected state and it loads fine in IE6/IE7. However when I scroll down the page and put the element outside of the viewport and then back in I get a broken image! I ...
I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...
Is it possible to reload a form after file-input change? I have a form where the user can chose an image for upload. I also have a php script which displays that image resized. I only wonder if it ...
I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!