一个可将onclick事件添加到页面上每个元素的小书签? [英] A bookmarklet that adds an onclick event to every element on the page?

查看:88
本文介绍了一个可将onclick事件添加到页面上每个元素的小书签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个书签,当您按下它时,页面上的所有元素都会得到一个onclick事件,该事件会调用以该元素作为参数或至少是id/class/something元素的函数,这可能吗?在那种情况下,我该怎么办?

I want 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冒泡.因此,只需绑定到<body>并检查event元素的target是什么(您需要对事件进行一些标准化处理,以使IE与众不同.)

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是实际的DOM元素,因此您可以过滤此时要过滤的任何内容.

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.)

这篇关于一个可将onclick事件添加到页面上每个元素的小书签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆