English 中文(简体)
鉴于四氯环乙烷的含量,如何找到治疗者的地位,以提取分辨率
原标题:Given a DIV w contenteditable, how to find the cursor position to extract a substring

Given:

<div id="comment_content_new" contenteditable="true" tabindex="0">
  <p>
     <span class="tag" id="tag30">theTag</span>&nbsp;@rac
  </p>
</div>

当 cur子在@rac之后,我可以找到 cur子:

储蓄Range = 窗口。

Which provides:

Range
 collapsed: true
 commonAncestorContainer: Text
 endContainer: Text
 endOffset: 5
 startContainer: Text
 startOffset: 5

我想能够做的是,就治疗者的立场而言,一切都向后退,直到第一个@为止。 鉴于这一范围使我错失了治疗师,我不得不使用我所尝试的方式。

text = $( #comment_content_new p ).text();
trigger =  @ ;
lastTriggerPosition = text.substring(0, cursorPosition).lastIndexOf(trigger);

关于我如何能够拉开这?? 我认识到,这并非易事! 感谢

问题回答
var str = text.substring(0, cursorPosition); // assumes this will give you the text in the div starting at the beginning and ending at the current cursor posistion.
var splitArr = str.split("@"); // splits the string into array of substrings with  @  as a delimiter.
var result = splitArr.pop(); // returns last item of array

如何?

String.split: http://www.w3schools.com/jsref/jsref_split.asp
Array.pop: http://www.w3schools.com/jsref/jsref_pop.asp

如果你想要一个坚固和经过测试的交叉浏览器解决方案,请

Among the methods there is one to get and set caret position.





相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签