使用Jquery将元素添加到页面时如何执行操作? [英] How to do an action when an element is added to a page using Jquery?

查看:91
本文介绍了使用Jquery将元素添加到页面时如何执行操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我添加类myClass的东西时,我想调用这个元素的一个函数。

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

这将是以下行:

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

...除此之外不存在。

... except this doesn't exist.

最好的方法是什么?

推荐答案

对您的问题的简短直接的回答是不可以做。但是在几分钟之前的评论中,我看到你想以不同的方式添加元素,并且有一种统一的处理这些新添加的项目的方法。我可以建议另一种方法吗? 触发自定义事件

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');

现在你的.live('load'...)将会起作用。

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

这篇关于使用Jquery将元素添加到页面时如何执行操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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