最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to do an action when an element is added to a page using Jquery? - Stack Overflow

programmeradmin0浏览0评论

When I add something of the class "myClass" I want to call a function on this element.

It would be something in the lines of:

jQuery(".error_message").live("load",function(){
  alert("test"+this);
});

... except this doesn't exist.

What's the best way of doing this?

When I add something of the class "myClass" I want to call a function on this element.

It would be something in the lines of:

jQuery(".error_message").live("load",function(){
  alert("test"+this);
});

... except this doesn't exist.

What's the best way of doing this?

Share Improve this question asked Sep 29, 2009 at 2:36 marcggmarcgg 66.4k53 gold badges182 silver badges234 bronze badges 4
  • How are you adding the elements to the DOM? – Colin Commented Sep 29, 2009 at 2:59
  • I load a some new html via ajax and throw it in the DOM – marcgg Commented Sep 29, 2009 at 3:00
  • There are different ways I could add these elements, that's why I'd like to avoid to have a callback on these functions. – marcgg Commented Sep 29, 2009 at 3:01
  • This is something I've wanted to know for Greasemonkey scripts too... – Kip Commented Sep 29, 2009 at 3:04
Add a comment  | 

1 Answer 1

Reset to default 17

The short, direct answer to your question would be "no can do." But from your comment a few minutes ago, I see that you want to add elements in different ways and have one unified method of handling these newly added items. May I suggest another approach? Trigger custom events.

The way it would work is like this: everywhere you load the error-message element, you add one line when it's done:

 $('.error_message').trigger('load');

Now your .live('load'...) thing will work.

发布评论

评论列表(0)

  1. 暂无评论