jQuery的不能访问附加元素 [英] jQuery can't access append element
本文介绍了jQuery的不能访问附加元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样的一个函数里:
I have this inside a function:
$('#users').append($("<div id='usuarios'>").text(mensaje[1][i]));
我要创建这个新的℃的onclick事件; DIV&GT;
。我怎样才能做到这一点?
我试图像这样的功能之外:
I want to create an onclick event for this new <div>
. How can I achieve this?
I'm trying it like this outside the function:
$(document).ready(function(){
$('#usuarios').click(function() {
alert("click");
});
});
不过,这是行不通的。
But it doesn't work.
推荐答案
有关jQuery的1.7+可以使用的。在()
For jQuery 1.7+ you can attach an event handler to a parent element using .on()
$(document).ready(function(){
$('body').on('click', '#usuarios', function() {
alert("click");
});
});
或更妙的是:
$(document).ready(function(){
$('#users').on('click', '#usuarios', function() {
alert("click");
});
});
这篇关于jQuery的不能访问附加元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文