gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 15:35:05 25 4
gpt4 key购买 nike

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

最佳答案

这取决于您尝试为哪个元素获取文本,但您可能想要做的是使用 mouseenter 事件。

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

编辑:

How to get a word under cursor using 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'
);

关于javascript - 将鼠标悬停在文本上时检索文本值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9864795/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com