简体   繁体   English

鼠标突出显示单词

[英]Words highlight on mouse over

Do you know the most efficient way to highlight word and add onmouseover event? 您是否知道突出单词并添加onmouseover事件的最有效方法?

I have text and I want to make somekind of word explanation field, so when user put his cursor on the word, I call AJAX to dictionary and show him meaning. 我有文字,我想制作一些单词解释字段,所以当用户将光标放在单词上时,我将AJAX称为字典并显示其意义。

I have two ideas: 1) Before showing text, put each word to <span onmouseon="my_foo("word");"> wrapper. 我有两个想法:1)在显示文本之前,将每个单词放到<span onmouseon="my_foo("word");"> wrapper。 For example: 例如:

<span onmouseon="my_foo("Hello");">Hello</span>
<span onmouseon="my_foo("world");">world</span>

But I think this will seriously overload the page. 但我认为这会严重超载页面。

2) When user hold cursor for more than 0.5 sec in one area, I get pointer coordinates, calcuate what word is shown (I do not know if is possible) and do AJAX call. 2)当用户在一个区域内按住光标超过0.5秒时,我得到指针坐标,计算显示的单词(我不知道是否可能)并进行AJAX调用。

What do you think is better, more easier to code? 您认为更好,更容易编码的内容是什么?

First highlight the word under the cursor then show the meaning of it, check my fiddle: http://jsfiddle.net/shahe_masoyan/z7nkU/1/ 首先突出光标下的单词然后显示它的含义,检查我的小提琴: http//jsfiddle.net/shahe_masoyan/z7nkU/1/

HTML HTML

    <div>
        <span>This text is a test text</span>
    </div>

JavaScript JavaScript的

$('div').each(function() {
    var $this = $(this);
    $this.html($this.text().replace(/\b(\w+)\b/g, "<span>$1</span>"));
});

$('div span').hover(
    function() { 
        $(this).css('background-color','#ffff66'); 
        alert(getTheMeaningOfTheWord($(this).html()));
    },
    function() { 
        $(this).css('background-color',''); 
    }
);

function getTheMeaningOfTheWord(word){

    return word+' = theMeaning';
}

I think 1 is simple and can be done this way: 我认为1很简单,可以这样做:

Fiddle: http://jsfiddle.net/9d227/ 小提琴: http//jsfiddle.net/9d227/

HTML HTML

<div id="yourDiv" style="Display: none;">your Text Here</div>
<div id="yourActualDiv"></div>

JQuery JQuery的

var wordArray = $('#yourDiv').html().split(' ');
var totalString = "";
for(var i=0;i<wordArray.length;i++)
    totalString += "<span>" + wordArray[i] + " </span>";
$('#yourActualDiv').html(totalString);
var ConcurrenyFlag = 0;
$('#yourActualDiv span').mouseover(
    function()
    {
       if(ConcurrenyFlag == 0)
       {
           ConcurrenyFlag = 1;
           // Your code here.
           ConcurrenyFlag = 0;
       }
    }
)

This way, you only need to put your text in the yourDiv . 这样,您只需将文本放在yourDiv The javascript code will handle the rest for you. javascript代码将为您处理剩下的事情。
Hope this helps you. 希望这对你有所帮助。

The ConcurrenyFlag will help in reducing the load on client as only one code part will run at a time. ConcurrenyFlag将有助于减少客户端的负载,因为一次只能运行一个代码部分。

Something like this should do something close to what you're looking for. 这样的事情应该做一些接近你正在寻找的事情。

$('.addWordDictionary').html('<span>'+$('.addWordDictionary').html().replace(/ {1,}/g, '</span> <span>')+'</span>');

But, as you said it may be client intensive especially if there is a lot of text. 但是,正如你所说,它可能是客户密集型的,特别是如果有很多文本。

You can use Lettering.js to solve this 您可以使用Lettering.js来解决此问题

http://letteringjs.com/ http://letteringjs.com/

Simplified example: 简化示例:

http://jsfiddle.net/Zzxpx/ http://jsfiddle.net/Zzxpx/

HTML: HTML:

<div class="w">"You will die of suffocation, in the icy cold of space"<div>

JS: JS:

$(".w").lettering('words');

I've made this stuff : http://jsfiddle.net/wared/eAq9k/ . 我做了这些东西: http//jsfiddle.net/wared/eAq9k/ Requires a textarea and works by clicking instead of hovering. 需要textarea并通过点击而不是悬停来工作。 More details about getCaret() here : https://stackoverflow.com/a/263796/1636522 . 有关getCaret()更多详细信息,请访问: https//stackoverflow.com/a/263796/1636522

<textarea readonly>Lorem ipsum</textarea>
textarea {
    display: block;
    width: 100%;
    border: none;
    resize: none;
    outline: none;
    margin: .5em 0;
}
$(function () {
    var el = $('textarea'),
        text = el.text();
    el.click(function () {
        var i = getCaret(this),
            w = getWord(text, i);
        $('p').text(i + ' : "' + w + '"');
    });

    // https://stackoverflow.com/a/995374/1636522

    el[0].style.height = '1px';
    el[0].style.height = 25 + el[0].scrollHeight + 'px';
});

function getWord(s, i) {
    var r = /\s/g;
    while (i && !r.test(s[--i])) {}
    r.lastIndex = i && ++i;
    return s.slice(i, (
        r.exec(s) || { index: s.length }
    ).index);
}

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

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