Phiên bản đầy đủ: jsB@nk » Ứng dụng » Ngắt dòng
URL: https://www.javascriptbank.com/line-wrapper-script.html
Hiệu ứng sẽ tự động thêm vào các dòng enter vào đoạn văn bản gốc.
Phiên bản đầy đủ: jsB@nk » Ứng dụng » Ngắt dòng
URL: https://www.javascriptbank.com/line-wrapper-script.html
<SCRIPT LANGUAGE="JavaScript"><!-- Beginfunction showLines(max, text) {max--;text = "" + text;var temp = "";var chcount = 0; for (var i = 0; i < text.length; i++) // for each character ... { var ch = text.substring(i, i+1); // first charactervar ch2 = text.substring(i+1, i+2); // next characterif (ch == '\n') // if character is a hard return{ temp += ch;chcount = 1;}else{if (chcount == max) // line has max chacters on this line{temp += '\n' + ch; // go to next linechcount = 1; // reset chcount}else // Not a newline or max characters ...{temp += ch;chcount++; // so add 1 to chcount } }}return (temp); // sends value of temp back}// End --></script><!-- This script downloaded from www.JavaScriptBank.com Come to view and download over 2000+ free javascript at www.JavaScriptBank.com-->
<form name=form1><textarea name=text1 rows=15 cols=50>This is just an example of a long textbox entry that just went on and on and on and on..... The visitor did not hit <enter> when entering this information so it continued off the right side of the textarea box. Notice that hitting <enter> after each line, like this:This is on another lineAnd so is this one.....Still wraps correctly. Neat!</textarea><br><input type=button value="Wrap Lines to 50 Spaces"onClick="this.form.text1.value = showLines(50, this.form.text1.value)"><br><br><textarea name=text2 rows=15 cols=50 wrap=virtual>This is another example, but this time the textarea box has the "wrap=virtual" attribute, which makes each line wrap in the box rather than scrolling out the right side. The script also correctly deals with this type of box. Like before, notice that hitting <enter> after each line, like this:This is on another lineAnd so is this one.....Still wraps correctly. Neat!</textarea><br><input type=button value="Wrap Lines to 50 Spaces"onClick="this.form.text2.value = showLines(50, this.form.text2.value)"></form><!-- This script downloaded from www.JavaScriptBank.com Come to view and download over 2000+ free javascript at www.JavaScriptBank.com-->