开发者

A bookmarklet that adds an onclick event to every element on the page?

I wan开发者_运维百科t to make bookmarklet, and when you press it all the elements on the page gets an onclick event that calls a function with the element as a parameter or at least the elements id/class/something, is that possible? In that case, how would I do it?


click bubbles. So just bind to the <body> and check what the target of the event element is (you'll need to do a bit of event normalization to get around IE being different from everyone else.)

event.target is the actual DOM element, so you can filter on anything that you want to filter on at that point.

This has the advantage that it doesn't matter how many elements are on the page, you only need one event handler for all of them. (Which means performance will be better on pages with hundreds or thousands of elements.)


javascript:void( document.body.onclick = function (event) { event = event || window.event; var element = event.target || event.srcElement; alert(element.textContent) } )     
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜