我想要触发像第一行或最后一行离开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;
答案 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>