我是否可以等待用户单击链接,并在单击链接时获得链接的文本?
也许通过使用onClick?
如果您的意思是为用户正在浏览的页面中的链接处理点击事件,那么这就是:
// 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);
}
}
使用 jQuery 非常简单:
<script>
$(document).ready(function(){
$("a").click(function(){alert($(this).text());});
});
</script>
当然,除了提醒文本,您可能还想做一些事情。