开发者

How to use Sly (or any other selector engine) to set event handlers?

Supposing I've got multiple div's with the same class, I could do something like :

$('.className').click(f) in Jquery or other frameworks

However, I was trying to use Sly as my se开发者_开发问答lector engine and wanted to do something like :

Sly.search('.className').click(f)

I'm not sure as to how the binding in Jquery works :

If I use the above JQuery, does an eventhandler get attached to all of the found divs individually? Or does it do something else to optimize and "tell" the browser specifically to link this function f to the onclick for the className (instead of manually telling the browser to attach the same function to each of the found divs)

Is there a way to achieve the above in Sly (or other Selector Engines) without using iteration over the elements returned ?


It will add a handler to each object currently in the set.

With the default Sizzle engine, you can call .live to add a single handler to <body> which catches all bubbled events and forwards them to any register events for matching selectors (what you're asking for).

You should not worry about performance issues; the point of .live is to affect elements that are created later.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜