简体   繁体   English

鼠标结束时,单词会突出显示

[英]Words highlight when mouse is over

I need some kind of help. 我需要一些帮助。 How is it possible to highlight one word in my own paste text. 如何在我自己的粘贴文本中突出显示一个单词。

Like I have <textarea></textarea> , where I can paste text or just one sentence and when mouse is over one word it is being highlighted like here by Damovisa: http://jsfiddle.net/5gyRx/ 就像我有<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',''); }
);

Regards 问候

You can't if you look at what he does you will see that he wraps every word in a span tag. 如果你看看他做了什么,你就不会看到他将每个单词包含在span标签中。 And then does what ever you need to. 然后做你需要的。

The only way you could do this is if you took it out of the textarea, and wrapped it in a span tag. 你能做到这一点的唯一方法就是你把它从textarea中拿出来,然后把它包裹在span标签中。 Similar to how things like tagit works. 类似于tagit的工作原理。

You won't be able to do it with a textarea because of HTML tags. 由于HTML标记,您将无法使用textarea

However, you can use a <div contenteditable="true"> to "simulate" a textarea. 但是,您可以使用<div contenteditable="true">来“模拟”textarea。

HTML HTML

<div contenteditable="true">Each word will be wrapped in a span.</div>
<div contenteditable="true">A second paragraph here.</div>
Word: <span id="word"></span>

JS JS

// wrap words in spans
$('div[contenteditable=true]').each(function() {
    var $this = $(this);
    $this.html($this.text().replace(/\b(\w+)\b/g, "<span>$1</span>"));
});

// bind to each span
$('div[contenteditable=true] span').hover(
    function() { $('#word').text($(this).css('background-color','#ffff66').text()); },
    function() { $('#word').text(''); $(this).css('background-color',''); }
);

DEMO: JSFiddle 演示: JSFiddle

Man, you guys are fast !! 伙计,你们快! - anyhow, as I made it ( was fun ) might as well share it - 无论如何,正如我所做的那样(很有趣)也可以分享它

  • yes Like Jonsuh answers well - here using contenteditable 是的,像Jonsuh的答案很好 - 这里使用contenteditable

DEMO: http://jsfiddle.net/P7S3Q/1/ 演示: http//jsfiddle.net/P7S3Q/1/

Perhaps an addition here is picking up on being able to paste/change text . 也许这里的一个补充就是能够粘贴/更改文本。

HTML: HTML:

<div class="wordscontainer" contenteditable="true">
    Paste words here ...
</div>

<div id="word"></div>

JS JS

function setHover() {
/* we could do to unbind here before re binding */
$('.wordscontainer span').hover(
    function() { 
        $('#word').text($(this).css('background-color','#ffff66').text()); },
    function() { 
         $('#word').text(''); $(this).css('background-color',''); }
);
}

$(".wordscontainer").keyup(function() {

    var $this = $(this);
    $this.html($this.text().replace(/\b(\w+)\b/g, "<span>$1</span>"));
    setHover();

});

SOME CSS used for display/demo 一些CSS用于显示/演示

.wordscontainer { 
     padding:10px; margin:10px; 
     border:2px solid #999; min-height:400px;
 }
#word { padding:10px margin:10px; font-size:20px; }

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

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