簡體   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