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

时间:2009-05-20 22:00:53

标签: javascript firefox hyperlink firefox-addon onclick

我是否可以等待用户点击链接,点击链接后会获得链接的文字?

也许使用onClick?

2 个答案:

答案 0 :(得分:6)

如果您的意思是处理用户正在浏览的页面中的链接的点击事件,那么这就是:

// 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);
   }   
} 

答案 1 :(得分:1)

使用jQuery非常简单:

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

当然,除了提醒文本之外,您可能还想做其他事情。