当用户在jQuery中将光标拖动到某段文本时如何突出显示该段文本

时间:2019-05-12 09:47:00

标签: javascript jquery cursor highlight

当用户在jQuery中将光标拖到某个文本上时,我无法突出显示该文本

我尝试使用jQuery给您的.select()函数,但似乎不适用于

这是一个例子

<div>
  Some random text <span class="highlighted_text">Highlighted text</span>
</div>

当用户将光标拖动到某段文字上时,它会包裹在标签中所拖动的文字上

谢谢

阿纳夫

1 个答案:

答案 0 :(得分:0)

只需执行以下操作,即可使用CSS突出显示它:

CSS:

.highlighted_text:hover {
    background-color: blue;
    color: white;
}

HTML:

<div>
  Some random text <span class="highlighted_text">Highlighted text</span>
</div>

但是,如果您希望在鼠标失去对文本部分的关注之后仍然突出显示,则必须使其更加复杂,例如:

CSS:

@keyframes hover {
  0% {
    color: #000;
  }
  100% {
    background-color: #00F;
    color: #FFF;
  }
}

.highlighted_text {
  animation-name: hover;
  animation-duration: 1000ms;
  animation-fill-mode: both;
  animation-play-state: paused;      
}

.highlighted_text:active,
.highlighted_text:focus,
.highlighted_text:hover {
  animation-play-state: running;
}

HTML:

<div>
  Some random text <span class="highlighted_text">Highlighted text</span>
</div>