jQuery:不能只选择附加的元素 [英] jQuery: Can't select just appended element

查看:87
本文介绍了jQuery:不能只选择附加的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是事件发生的事情:

$('div#pages').append($('<div class="page" id="test">...</div>'));

然后,在另一个事件中,它无法执行此操作:

And then, on another event it fails doing this:

var page = $('div.page#test'); // returns empty array

我已经调试过,附加的html在附加后出现在文档结构中,但未能被选中。在浏览器控制台中执行相同操作非常有效。

可能是什么问题?

I've debugged, and the appended html appears in the document structure after appending, but fails to get selected. Doing the same in browser console works perfectly.
What could be the problem?

推荐答案

使用 .find() http://api.jquery.com/find

var page = $('div#pages').find('div.page#test');

这篇关于jQuery:不能只选择附加的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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