如果线条没有线路断线,是否可以知道我的光标是否在textarea的第一行或最后一行?

时间:2018-04-26 13:01:45

标签: javascript jquery html5

我想要触发像第一行或最后一行离开textarea。问题是我不能使用换行符。因为文本可以在没有硬线断开的情况下进行换行。



<!DOCTYPE html>
<html>
   <head>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
   </head>
   <body>
      <form action="/action_page.php">
         <textarea rows="2" cols="20" name="usrtxt" wrap="soft">
         I'am only one line without line breaks. Look joe I don't break but I wrap.
         </textarea>
      </form>
   </body>
</html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

<!DOCTYPE html>
<html>
   <body>
      <form action="/action_page.php">
         <textarea rows="2" cols="20" name="usrtxt" wrap="soft" onkeyup="getLineNumber(this, document.getElementById('lineNo'));" onmouseup="this.onkeyup();">I'am only one line without line breaks. Look joe I don't break but I wrap.</textarea>
      </form>
      <div id="lineNo">You are on linenumber</div>
      <script>
         function getLineNumber(textarea, indicator) {
             indicator.innerHTML = textarea.value.substr(0, textarea.selectionStart).split("\n").length;
         }
      </script>
   </body>
</html>