【问题标题】:JavaScript execCommand("HiliteColor") unhighlightJavaScript execCommand("HiliteColor") 取消突出显示
【发布时间】:2019-01-04 14:58:25
【问题描述】:

JavaScript execCommand("HiliteColor") 通过添加跨度非常好地添加了高亮,但我希望能够通过检查所选文本是否在高亮显示的跨度中来动态取消高亮显示文本。然后有一个问题,只有一半的选定文本在一个跨度内。我尝试自己添加跨度并尝试通过以下方式取消突出显示它们:

document.getElementsByClassName('highlight').remove();

alert(window.getComputedStyle(document.getElementById("pages"), null).getPropertyValue('background-color'));

alert(document.getElementById("pages").style.backgroundColor);

只是看看我是否可以检查背景然后突出显示,或者我是否可以删除类突出显示。

我的项目在 codepen 上:https://codepen.io/pokepimp007/pen/wxGKEQ

回答

我创建了一个在单击按钮时采用颜色参数的函数。单击删除突出显示按钮时,它会发送参数颜色“透明”:

function Highlight(color) {
  document.designMode = "on";
  var sel = window.getSelection();
  sel.removeAllRanges();
  var range = document.createRange();
  range.setStart(editor.startContainer, editor.startOffset);
  range.setEnd(editor.endContainer, editor.endOffset);
  sel.addRange(range);
  if (!sel.isCollapsed) {
    if (!document.execCommand("HiliteColor", false, color)) {
      document.execCommand("BackColor", false, color);
    }
  }
  sel.removeAllRanges();
  document.designMode = "off";
}

【问题讨论】:

标签: javascript jquery html css highlight


【解决方案1】:

我看到你使用 jQuery,所以在你的帖子中添加了 jQuery 标签。

这就是诀窍。

$('#removeHighlight').on('click', function(){
   $('.highlight').each(function(){
       $(this).replaceWith($(this).text());
   })
})
.highlight {
  background: yellow;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>This is a stupid bit of text with <span class="highlight">highlight_1</span> in it to display the power of jquery to do stuff like removing  <span class="highlight">highlight_2</span> in a html document. Go on and press the button to see the <span class="highlight">highlight_3</span> magic.</p>
<button id="removeHighlight">Remove</button>

如果您只想删除一个突出显示,请执行此操作。

$('#removeHighlight').on('click', function(){
     $('.highlight').first().replaceWith($('.highlight').first().text());
})
.highlight {
  background: yellow;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>This is a stupid bit of text with <span class="highlight">highlight_1</span> in it to display the power of jquery to do stuff like removing  <span class="highlight">highlight_2</span> in a html document. Go on and press the button to see the <span class="highlight">highlight_3</span> magic.</p>
<button id="removeHighlight">Remove 1</button>

或者如果你想在点击时删除它

$('p').on('click', '.highlight', function(){
   $(this).replaceWith($(this).text());
})
.highlight {
  background: yellow;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>This is a stupid bit of text with <span class="highlight">highlight_1</span> in it to display the power of jquery to do stuff like removing  <span class="highlight">highlight_2</span> in a html document. Go on and press the button to see the <span class="highlight">highlight_3</span> magic.</p>

【讨论】:

  • 这仍然会删除所有亮点。我只想一次删除一个亮点。
  • @ZarenWienclaw 认为您应该能够通过小幅调整自己做到这一点。但是添加了2个sn-ps。 1 在按钮单击时删除 1 个突出显示。一个 sn-p 删除您单击的突出显示。
  • 我认为 OP 想在用鼠标选择文本时删除突出显示。即你有&lt;span class...&gt;highlight&lt;/span&gt;。如果用户仅选择单词的这一部分,则可能仅删除 high 的突出显示。当您同时使用突出显示和未突出显示的文本时,这会变得更加复杂。 你好 world 用户选择 lo wor 将根据我的理解删除 wor 上的突出显示
  • 谢谢!这真的帮助我弄清楚了!
猜你喜欢
  • 2015-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-04
相关资源
最近更新 更多