前往刚添加到DOM一个jQuery元素访问 [英] Getting access to a jquery element that was just appended to the DOM

查看:132
本文介绍了前往刚添加到DOM一个jQuery元素访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是追加这是对像DOM元素:

I am simply appending an element that is on the DOM like:

$("#div_element").append('<a href="#">test</a>');

紧接着我追加它,我需要访问我只是为了一个点击功能绑定向它提出的元素,我想:

Right after I append it I need access to the element I just made in order to bind an click function to it, I tried:

$("#div_element").append('<a href="#">test</a>').click(function(){alert("test")});

但上面没有工作。我唯一​​能标识的元素,但似乎像一个有点不务正业的时候也许有一种方法可以让我得到之后我追加它的元素。

But the above didn't work. I could uniquely id the element but that seems like a bit to much work when perhaps there is a way I can get the element right after I append it.

推荐答案

您可以这样做:

var el = $('<a href="#">test</a>');

$("#div_element").append(el);

el.click(function(){alert("test")});

这篇关于前往刚添加到DOM一个jQuery元素访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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