简体   繁体   English

Javascript firefox扩展以获取链接周围的文本

[英]Javascript firefox extension to get the text around the link

Is it possible for me waiting for a user to click a link, and upon clicking the link the link's text would be obtained? 我是否可以等待用户单击链接,并且单击链接后将获得链接的文本?

Maybe by using onClick? 也许通过使用onClick?

If you mean handling the click event for the links in the page that the user is browsing then this is how: 如果您要处理用户正在浏览的页面中的链接的click事件,那么这是怎么做的:

// handle the load event of the window  
window.addEventListener("load",Listen,false);  
function Listen()  
{   
gBrowser.addEventListener("DOMContentLoaded",DocumentLoaded,true);  
}  

// handle the document load event, this is fired whenever a document is loaded in the browser. Then attach an event listener for the click event  
function DocumentLoaded(event) {  
 var doc = event.originalTarget;
 doc.addEventListener("click",GetLinkText,true);  
}  
// handle the click event of the document and check if the clicked element is an anchor element.  
function GetLinkText(event) {  
   if (event.target instanceof HTMLAnchorElement) {  
    alert(event.target.innerHTML);
   }   
} 

This is very simple using jQuery: 使用jQuery,这非常简单:

<script>
    $(document).ready(function(){
        $("a").click(function(){alert($(this).text());});
    });
</script>

Of course, you'll probably want to do something other than alert the text. 当然,除了提醒文本外,您可能还想做其他事情。

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

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