[英]Parse html from variable into jQuery object
I am doing an ajax post that returns some html and I want to find out how many divs with a class of entry there are in the returned html and then loop through them. 我正在做一个返回一些html的ajax帖子,我想找出返回的html中有多少个具有一类条目的div,然后遍历它们。
$.post(url, { 'data' : 'qdewde' }, function(data) {
alert( $('entry', data).length );
$('.entry', data).each(function() {
// do something here
});
}, 'html');
html returned: html返回:
<!DOCTYPE html>
<html lang="en">
<head>
<body>
<div class="entry"><h1>Entry 1</h1></div>
<div class="entry"><h1>Entry 2</h1></div>
<div class="entry"><h1>Entry 3</h1></div>
</body>
</html>
The alert just outputs 0 though and I'm unable to loop through each .entry
object. 警报只是输出0,但我无法遍历每个.entry
对象。 What am I doing wrong? 我究竟做错了什么?
For this you can create a virtual div to hold the response then only you can get the length: 为此,您可以创建一个虚拟div来保存响应,然后只有您可以获取长度:
$.post(url, { 'data' : 'qdewde' }, function(data) {
var vd = $('<div>', {html:data});
alert(vd.find('.entry').length);
}, 'html');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.