【发布时间】:2014-03-17 06:02:48
【问题描述】:
我需要一些帮助。如何在我自己的粘贴文本中突出显示一个单词。
就像我有 <textarea></textarea> 一样,我可以在其中粘贴文本或只粘贴一个句子,当鼠标悬停在一个单词上时,它会像 Damovisa 一样在此处突出显示:http://jsfiddle.net/5gyRx/
<p>Each word will be wrapped in a span.</p>
<p>A second paragraph here.</p>
Word: <span id="word"></span>
// wrap words in spans
$('p').each(function() {
var $this = $(this);
$this.html($this.text().replace(/\b(\w+)\b/g, "<span>$1</span>"));
});
// bind to each span
$('p span').hover(
function() { $('#word').text($(this).css('background-color','#ffff66').text()); },
function() { $('#word').text(''); $(this).css('background-color',''); }
);
问候
【问题讨论】:
-
也许这会让你走上正轨 - strangeplanet.fr/work/jquery-highlighttextarea - 这只是一种模拟的方式,但在我看来,应该可以根据你的需要调整代码。
标签: javascript jquery html css