我让我的json返回html。下面显示的是ajax位:
$.ajax({
type: 'POST',
url: area.wrapper.attr('data-ajax'),
data: data_submit,
dataType: 'json',
success: function( data )
{
console.log(data.content);
}
});
console.log(data.content); 会返回如下所示的html:
<div class="gdlr-booking-room-wrapper" ><img src="" alt="" width="400" height="300" /></a></div><span class="gdlr-head">Start From</span><span class="gdlr-tail" id = "gdlr-currencyprice">$63.00 / Night</span></div><div class="clear"></div></div><div class="clear"></div></div></div>
<div class="gdlr-booking-room-wrapper" ><img src="" alt="" width="400" height="300" /></a></div><span class="gdlr-head">Start From</span><span class="gdlr-tail" id = "gdlr-currencyprice">$68.00 / Night</span></div><div class="clear"></div></div><div class="clear"></div></div></div>
<div class="gdlr-booking-room-wrapper" ><img src="" alt="" width="400" height="300" /></a></div><span class="gdlr-head">Start From</span><span class="gdlr-tail" id = "gdlr-currencyprice">$58.00 / Night</span></div><div class="clear"></div></div><div class="clear"></div></div></div>
<div class="gdlr-booking-room-wrapper" ><img src="" alt="" width="400" height="300" /></a></div><span class="gdlr-head">Start From</span><span class="gdlr-tail" id = "gdlr-currencyprice">$50.00 / Night</span></div><div class="clear"></div></div><div class="clear"></div></div></div>
从上面的html响应中,我想捕获该类下的价格:
<span class="gdlr-tail" id = "gdlr-currencyprice">$63.00 / Night</span>
现在,下一步应该是从类中检索值,这应该是要走的路:
$(“。gdlr-tail”)。each(function(i){}
所以我的问题是,如何从我的ajax响应: data.content ,我应该捕获我的类属性(gdlr-tail)以将其传递给我的迭代函数来访问我的值?希望我的问题很清楚
答案 0 :(得分:3)
使用jQuery,你可以这样做:$(data.content).find('。gdlr-tail')。each(...)
答案 1 :(得分:2)
试试这个
$(data.content)
.find('.gdlr-tail')
.each(function(i,el){
alert(el);
});
答案 2 :(得分:0)
使用RegExp提取值:
var html = '<div class="gdlr-booking-room-wrapper" ><img src="" alt="" width="400" height="300" /></a></div><span class="gdlr-head">Start From</span><span class="gdlr-tail" id = "gdlr-currencyprice">$63.00 / Night</span></div><div class="clear"></div></div><div class="clear"></div></div></div><div class="gdlr-booking-room-wrapper" ><img src="" alt="" width="400" height="300" /></a></div><span class="gdlr-head">Start From</span><span class="gdlr-tail" id = "gdlr-currencyprice">$68.00 / Night</span></div><div class="clear"></div></div><div class="clear"></div></div></div><div class="gdlr-booking-room-wrapper" ><img src="" alt="" width="400" height="300" /></a></div><span class="gdlr-head">Start From</span><span class="gdlr-tail" id = "gdlr-currencyprice">$58.00 / Night</span></div><div class="clear"></div></div><div class="clear"></div></div></div><div class="gdlr-booking-room-wrapper" ><img src="" alt="" width="400" height="300" /></a></div><span class="gdlr-head">Start From</span><span class="gdlr-tail" id = "gdlr-currencyprice">$50.00 / Night</span></div><div class="clear"></div></div><div class="clear"></div></div></div>';
var result = html.match(/\$[\d\.]+ \/ [^<]+/gi);
console.log(result);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;