2009-05-20 18 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); 
    } 
}