在页面中的任何地方的javascript中找到鼠标指针下的单词

use*_*507 2 javascript jquery

我可以通过使用此代码找到元素的文本.但是我想在我的页面中找到一个单词,如果鼠标在该单词上停留一段时间.

$(document).bind("mouseover", function (e) {
    var event = e;
    setTimeout(function () { $(event.target).contents().each(function(index, elem) {
        if( elem.nodeType === 3 && $.trim(elem.nodeValue).length ) {
            var text = $.trim(elem.nodeValue);
            text = text.split(" ");
            console.log($.trim(elem.nodeValue));
            //alert($.trim(elem.nodeValue));
            return false;
         }
      });
   }, 5000);
Run Code Online (Sandbox Code Playgroud)

});

got*_*les 5

这可能不是有史以来最好的代码,但它可能会让你走上正确的轨道,取决于andyb的评论.

就像是

$(".first").on("hover",function(){

  var words = $(this).text().split(" ");

  var fullText = "";

  for(i=0;i<words.length;i++){
    words[i] = "<span>"+words[i]+"</span> "; 
    fullText += words[i];
  }  

  $(this).text("").append(fullText);

  $(this).children("span").on("hover", function(){
    $(".second").text($(this).text());
  });

});
Run Code Online (Sandbox Code Playgroud)

虽然查看工作示例是您最好的选择.