如何检查光标是否在textarea内的空行?
如何检查光标是否位于textarea内的空行?
我不在乎换行。 我关心通过按Enter键创建的行。
$('textarea').on("change keyup", function() { if (cursor is on an empty line or the the line has just spaces) { console.log("empty"); } else { console.log("has stuff"); } });
例如:
这将记录“有东西”
上面的行会记录“空”,这行会记录“有东西”
以下应该可以工作,但不会在旧版本的IE中使用。
// on(..., function() { if( this.value.substr(0,this.selectionStart).match(/(?:^|\r?\n\s*)$/) && this.value.substr(this.selectionStart).match(/^(?:\s*\r?\n|$)/)) { console.log("empty"); }
EDIT搜索现在显式查找换行符,其他空格可选。