简体   繁体   English

JQuery / Javascript-突出显示文本并在周围包裹标签,而无需更改html结构

[英]JQuery/Javascript - Highlight text and wrap tags around, without changing the html structure

I am trying to make a program where users can highlight text to annotate. 我正在尝试制作一个程序,用户可以在其中突出显示要注释的文本。 I want the text to stay the same no matter where the user highlights text. 无论用户在何处突出显示文本,我都希望文本保持不变。

Something similar to this website: http://genius.com/Future-hater-shit-lyrics 与此网站相似的网站: http//genius.com/Future-hater-shit-lyrics

When I try to highlight text inside some html-tags it works just as I want. 当我尝试突出显示某些html标签内的文本时,它可以按照我的意愿工作。 But when I highlight text over one or more html-tags the html structure changes. 但是,当我在一个或多个html标签上突出显示文本时,html的结构就会改变。 I would like this to work with all html-tags, because the html structure may vary. 我希望这可以与所有html标签一起使用,因为html结构可能有所不同。 Is there a way to prevent this? 有办法防止这种情况吗?

Here is a fiddle I made in case I am not being clear: https://jsfiddle.net/4a1x4t3y/ 这是我弄不清楚的小提琴: https : //jsfiddle.net/4a1x4t3y/

Sorry for my english. 对不起我的英语不好。

function highlightText(){

var selectedText, selectionEl, sel, range;

if (typeof window.getSelection != "undefined") {
    selectedText = window.getSelection().toString();
} 
else if (typeof document.selection != "undefined" && document.selection.type == "Text") {
    selectedText = document.selection.createRange().text;
}

if (document.selection && document.selection.createRange) {

    range = document.selection.createRange().duplicate();
    range.collapse(false);

} else if (window.getSelection) {
    sel = window.getSelection();

    if (sel.getRangeAt) {
        range = sel.getRangeAt(0).cloneRange();

    } else {
        range.setStart(sel.anchorNode, sel.anchorOffset);
        range.setEnd(sel.focusNode, sel.focusOffset);

        if (range.collapsed !== sel.isCollapsed) {
            range.setStart(sel.focusNode, sel.focusOffset);
            range.setEnd(sel.anchorNode, sel.anchorOffset);
        }
    }

  var a = document.createElement('a');
  a.textContent = selectedText;
  a.href = "#";

  range.deleteContents();
  range.insertNode(a);
  range.collapse(false);
}
}

$("#article").mouseup(function(){
   highlightText();
});

There are two ways you can tackle this. 有两种解决方法。

  1. Use < br /> tags to separate paragraphs instead of < p > tags. 使用<br />标记而不是<p>标记来分隔段落。

    Ex: https://jsfiddle.net/asagex/4a1x4t3y/1/ 例如: https//jsfiddle.net/asagex/4a1x4t3y/1/

  2. When you select text create multiple a tags. 选择文本时,请创建多个标签。

This: 这个:

<p> My text of (SELECT START) doom.</p>
<p>More text (SELECT END) and what not </p>

Would become: 会成为:

<p>My text of <a href="">doom.</a ></p>
<p><a href="">More text</a> and what not </p>

The second one is easier but will only work for paragraph tags. 第二个比较容易,但仅适用于段落标签。 If you intend to have span tags as well then the second option is your best bet. 如果您也打算使用span标签,那么第二种选择是最好的选择。

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

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