我正在尝试做的是:
我知道一些基本的jquery,但似乎无法理解所需的较低级别的javascript。这是我到目前为止的内容:
$("textarea").dblclick(function() {
//TODO: Prevent selection
//TODO: Get the line number??
//TODO: prepend a dash to the line
// or replace the line with itself
// plus the dash at the front??
});
您可能需要做很多事情,但是这样的事情足以让您入门:
$("textarea").dblclick(function() {
//first, get the position of the cursor
var cursorPosition = $(this).prop("selectionStart");
//get the text value at the cursor position
var textValue = $(this).val().substr(cursorPosition,1);
//use a loop to look backward until we find the first newline character \n
while(textValue != '\n' && cursorPosition >= 0) {
cursorPosition--;
textValue = $(this).val().substr(cursorPosition,1);
}
//update the textarea, combining everything before the current position, a dash, and everything after the current position.
$(this).val(($(this).val().substr(0,cursorPosition+1) + '-' + $(this).val().substr(cursorPosition+1)))
});
您可以在此JS小提琴中看到一个示例:
http://jsfiddle.net/igor_9000/4zk5otvm/2/
您可能还需要添加更多内容,具体取决于您希望对该功能执行的操作以及要强制执行的限制,但这足以让您入门。希望有帮助!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句