繁体   English   中英

将鼠标悬停在文本上时检索文本值

[英]Retrieve text value when hover over text

我到处搜寻,但找不到。 所以我在这里问。 当鼠标位于html页面内该文本的顶部(悬停)时,我想获取文本。 我尝试使用getClientRect,但它们只给我坐标,而不给文本。 有人知道如何使用javascript或html吗? 谢谢。

如果可以访问jQuery,则可以使用.mouseover()事件处理程序。 您可以将其绑定到特定的DOM元素,然后通过val或innerhtml获取值。

这取决于您要获取文本的元素,但是可能要使用mouseenter事件。

$(parentselector).on({
    'mouseenter': function(){
            alert($(this).html()); // .html() or .val() depending on the element
        }
    },
    targetselector
);

编辑:

如何使用JavaScript在光标下获取单词?

像这样创建HTML

<html>
   <head></head>
   <body>
      <span class="word">Hello</span>
      <span class="word">I</span>
      <span class="word">am</span>
      <span class="word">new</span>
   </body>
</html>

然后在jQuery中类似这样。

$('body').on({
    'mouseenter': function(){
            var txt = $(this).html();
        }
    },
    'span.word'
);

暂无
暂无

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

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