2009-05-20 43 views
1

我可以等待用户点击链接,点击链接后链接的文本将被获取?Javascript firefox扩展以获得链接文本

也许通过使用onClick?

+0

通过“周围的链接文本”,你的意思是开之间的文本关闭标签? – ahockley 2009-05-20 22:10:35

+0

实际上意味着整个段落,但即使在标签之间的文本将是好的 – Lilz 2009-05-21 00:46:23

回答

1

这是使用jQuery非常简单:

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

当然,你可能会想要做的比提醒文字以外的东西。

+3

他想知道如何处理他在firefox扩展中的点击事件而不是在html页面 – 2009-05-20 22:17:57

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