JQuery访问动态创建的对象 [英] JQuery access dynamically created objects

查看:113
本文介绍了JQuery访问动态创建的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何接受动态生成的对象(div)。
我的意思是当 $(document).ready(function()开始时,输出中不存在DIVS。

How can I acceess objects(divs) that were dynamically generated. I mean DIVS that were not present in output when $(document).ready(function() started.

如果我这样做:

$('#click_me').click(function() {
    $('#container').append('<div id="clicker2">can you click on me?</div>');
});

$('#clicker2').click(function() {
    alert('hurray, it works');
});

clicker2不起作用

the clicker2 won't work

如何解决?
我打算创建多个动态,我想分配Jquery动作给themt。

How can I fix it? I'm intent to create more than one dynamically. and I want to assign Jquery actions to themt too.

推荐答案

.click在以后添加的跨度或div上工作时,您需要使用 .live()

.click() functions that aren't working on spans or divs that are added later, you'll need to use .live()

$("#clicker2").live("click", function(){
  # do stuff to spans currently existing
  # and those that will exist in the future
});

这篇关于JQuery访问动态创建的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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