简体   繁体   English

调用函数时,Textarea失去了焦点

[英]Textarea loses focus when calling a function

What I'm trying to achieve is to read the current word at the caret position. 我想要实现的是读取插入位置的当前单词。

Example: 例:

Hello| 你好| -> returns Hello - >返回Hello

Hel|lo -> returns Hello Hel | lo - >返回Hello

I have put the code inside an event handler which is onKeyup inside an if statement: 我已将代码放在if语句中的onKeyup事件处理程序中:

if( e.keyCode === 37 || e.keyCode === 39 ){
  console.log($(this).getWord());
}

When this is executed, it returns the current word, but the text area loses the focus. 执行此操作时,它将返回当前单词,但文本区域将失去焦点。

Jsfiddle 的jsfiddle

How is this supposed to work ? 这应该怎么样

Type word in the text area then click on left or right arrow keys, the current word will get displayed. 在文本区域中键入单词,然后单击向左或向右箭头键,将显示当前单词。

I would use textArea.selectionStart instead of mucking with mutating the selection itself. 我会使用textArea.selectionStart而不是改变选择本身。 This will work for you if Internet Exploder < 8 compatibility isn't a concern. 如果不考虑Internet Exploder <8兼容性,这将适用于您。

 var $mytextarea = $('#mytextarea'); $mytextarea.keyup(function(e) { if (e.keyCode === 37 || e.keyCode === 39) { $("#current").text(getWord()); } }); function getWord() { var textarea = $mytextarea[0], pos = textarea.selectionStart, raw = textarea.value, start = pos, end = pos; while (raw.charAt(start) !== ' ' && start > 0) { start--; } while (raw.charAt(end) !== ' ' && end < raw.length) { end++; } return raw.substring(start, end); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <textarea id="mytextarea">The quick brown fox jumps over the lazy dog.</textarea> <div> the current word is <span id="current"></span> </div> 

I changed your code a bit. 我改变了你的代码。 It no longer manipulates the cursor which means response time is a lot faster. 它不再操纵光标,这意味着响应时间要快得多。

Here is your updated code: 这是您更新的代码:

 $('#mytextarea').keyup(function (e) { //if (e.keyCode === 37 || e.keyCode === 39) { //get cursor position var caret = getCaretPosition(this); //get the end index of current word var endOfWord = this.value.slice(caret.end).match(/(\\s|$)/i).index + caret.end; //get current word var word = /\\S+$/.exec(this.value.slice(0,endOfWord)); //make sure word is not null word = word ? word[0] : ''; //remove punctuation word = word.replace(/(\\.|\\!|,|;|:|\\(|\\)|\\{|\\}|\\[|\\]|'|"|-$)/,''); $("#current").text(word); //} }); function getCaretPosition(ctrl) { var start, end; if (ctrl.setSelectionRange) { start = ctrl.selectionStart; end = ctrl.selectionEnd; } else if (document.selection && document.selection.createRange) { var range = document.selection.createRange(); start = 0 - range.duplicate().moveStart('character', -100000); end = start + range.text.length; } return { start: start, end: end } } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <textarea id="mytextarea"></textarea> <div> the current word is <span id="current"></span> </div> 

I do want to point out this is not my code and was copied from this fiddle . 我想指出这不是我的代码,而是从这个小提琴中复制而来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM