与跨度选择的功能相反

时间:2021-02-22 15:44:37

Hey Guys I got a function like this:

嘿伙计我有这样的功能:

  define(function () {
  'use strict';
  var sel, range, span;
  return function () {
    span = document.createElement("span");
    span.className = 'highlight';

    if (window.getSelection) {
      sel = window.getSelection();
      if (sel.rangeCount) {
        range = sel.getRangeAt(0).cloneRange();
        range.surroundContents(span);
        sel.removeAllRanges();
        sel.addRange(range);
       }
     }
   };
 });

It bascially surrounds my selection with a span. I would like to have an opposite of this method removes the span from the selected text. For example

它以跨度为基础围绕着我的选择。我希望与此方法相反,从所选文本中删除范围。例如

I have a div:

我有一个div:

<div>some text in here</div>

Lets say I applied the above on the word "here" of above:

假设我在上面的“here”这个词中应用了上述内容:

<div>some text in <span class="highlight">here</span></div>

I would like to have a method that removed the span from the word if I have it highlighted again.

如果我再次突出显示,我想有一个方法可以从单词中删除跨度。

1 个解决方案

#1


1  

As you have tagged jQuery. You can simply use combination of .contents() and .unwrap()

正如你已经标记了jQuery。你可以简单地使用.contents()和.unwrap()的组合

$('.highlight').contents().unwrap()

DEMO

#1


1  

As you have tagged jQuery. You can simply use combination of .contents() and .unwrap()

正如你已经标记了jQuery。你可以简单地使用.contents()和.unwrap()的组合

$('.highlight').contents().unwrap()

DEMO