这是我的代码:
<script type='text/javascript'>
$(document).ready(function() {
$('#test').load('http://foobar.com/view.php?id=1186', function() {
console.log($('.weekdates'));
var weeks = $('.weekdates');
weeks.each(function(i, val) {
alert($(this).text());
});
});
});
</script>
console.log
来电的结果是:
[prevObject: b.fn.b.init[1], context: document, selector: ".weekdates", jquery: "1.9.1", constructor: function…]
context: document
length: 0
prevObject: b.fn.b.init[1]
selector: ".weekdates"
__proto__: Object[0]
为什么在结果集中找不到元素的任何想法?它是否与动态加载HTML有关?
以下是添加的HTML:
<h3 class="weekdates"> 23 de enero - 29 de enero</h3>
答案 0 :(得分:1)
要直接回答你的问题,你可以在从ajax调用返回的HTML上使用jquery选择器,就像你将它加载到jQuery对象变量中一样...
<script type='text/javascript'>
$(document).ready(function() {
$('#test').load('http://foobar.com/view.php?id=1186', function(html) {
var $html = $(html);
});
});
</script>
但老实说,似乎你正试图循环结果。如果这是您从服务器返回的JSON,则需要$ .getJSON()
$.getJSON('http://foobar.com/view.php?id=1186', function(data) {
$.each(data, function(key, val) {
// deal with item
});
});