开发者

how to declare js event and function to every dom element with given tag and class even when these elements don't exist at the moment?

I need to 'decorate' every div.myclass with a functio开发者_如何学Gon in a way that css is doing it with style, that is some of these elements aren't on a page when this function fires, so I can't just find and loop trough them. Some of the elements are created from ajax requests, and some are changing classes.

In css when an element changes class it's style changes instantly. I also need a similar situation here when for example all divs with class1 would have declared one function and with class2 another, and when I do for example:

document.getElementsByClassName("class1")[0].setAttribute("class", "class2");

Behavior of that element should change instantly.

I prefer pure js, but if it's there a cleaner jquery or prototype way I also be glad.


Future Readers: .live() is deprecated and should no longer be used! In the previous cases where it was used .delegate() is a more preferable method.

https://stackoverflow.com/a/11115926/829835 here is what is wrong with .live()

The jQuery team itself also suggests this function is no longer used.


Check http://api.jquery.com/live/

"The .live() method is able to affect elements that have not yet been added to the DOM through the use of event delegation[...]"


I guess you need jQuery's live():

http://api.jquery.com/live/

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜