English 中文(简体)
给用户加标签加亮突出显示的文字吗?
原标题:Put a tags round user highlighted text?

我需要获得文本区域的用户选中区域, 然后在它周围插入 标签 。

我使用此选项获取用户选择区域 :

var textComponent = document.getElementById( article );
var selectedText;

if (document.selection != undefined)
{
    textComponent.focus();
    var sel = document.selection.createRange();
    selectedText = sel.text;
}

// Mozilla version
else if (textComponent.selectionStart != undefined)
{
    var startPos = textComponent.selectionStart;
    var endPos = textComponent.selectionEnd;
    selectedText = textComponent.value.substring(startPos, endPos)
}

现在,我知道我可以对用户选中的文本进行字符串搜索,并围绕它插入一个标记,但如果该用户选中的文本在文本中出现两次,例如,会发生什么情况。

你好,再见

如果用户突出显示您想要的链接的第二秒, 字符串的替换肯定会给您每个例子贴上标签 。

这样做的最佳方式是什么?

最佳回答

您可以为此使用我的""http://code.google.com/p/rangyinputs/" rel="nofoln noreferrer" >jQuery 插件 (http://ragyinputs.googlecode.com/svn/trunk/demos/textinputs_jquery.html" rel="nofoln noreferr"> demo :

$("#article").surroundSelectedText( <a href="foo.html"> , "</a>");

您也可以使用 < a href=" https://stackoverflow.com/ questions/263743/ how-to-get-caret-position- in-textreare/3373056#3373056#3373056"\\ code>getInputSection () 函数, 以获取所有主要浏览器的选择起始和结束字符索引, 然后在文本区域 s value 上进行字符替换 :

var sel = getInputSelection(textComponent);
var val = textComponent.value;
textComponent.value = val.slice(0, sel.start) +
                       <a href="foo.html">  +
                      val.slice(sel.start, sel.end) +
                      "</a>" +
                      val.slice(sel.end);
问题回答

为什么要抓取选中的文本? 您真正想要的是开始/ 结束位置 。

var textComponent = document.getElementById( article );
var selectedText;
var startPos;
var endPos;

// the easy way
if (textComponent.selectionStart != undefined)
{
    startPos = textComponent.selectionStart;
    endPos = textComponent.selectionEnd;
}
// the hard way
else if (document.selection != undefined)
{
    textComponent.focus();
    var sel = document.selection.createRange();
    var range = document.selection.createRange();
    var stored_range = range.duplicate();
    stored_range.moveToElementText(textComponent);
    stored_range.setEndPoint(  EndToEnd , range );
    startPos = stored_range.text.length - range.text.length;
    endPos = startPos + range.text.length;
} 

// add in tags at startPos and endPos
var val = textComponent.value;
textComponent.value = val.substring(0, startPos) + "<a>" + val.substring(startPos, endPos) + "</a>" + val.substring(endPos);

这一引用中修改的IE代码。

EDIT: Note Tim Down s comments about newlines. 可能使用他的解析,因为它更好。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签