我有一个页面,我想在不同的位置展示和刷新不同的广告。我通过AJAX成功获得PHP文件的响应,但无法将它们附加到特定的展示位置。
HTML CODE
<script>var ads = [];</script>
<div id="ad-728x90">fhhfgh</div>
<!-- Page Content -->
<div class="container">
<!-- Heading Row -->
<div class="row">
<div class="col-md-8">
<img class="img-responsive img-rounded" src="http://placehold.it/900x350" alt="">
</div>
<!-- /.col-md-8 -->
<div class="col-md-4">
<h1>Business Name or Tagline</h1>
<p>This is a template that is great for small businesses. It doesn't have too much fancy flare to it, but it makes a great use of the standard Bootstrap core components. Feel free to use this template for any project you want!</p>
<a class="btn btn-primary btn-lg" href="#">Call to Action!</a>
</div>
<!-- /.col-md-4 -->
</div>
<!-- /.row -->
<hr>
<!-- Call to Action Well -->
<div class="row">
<div class="col-lg-12">
<div class="well text-center">
This is a well that is a great spot for a business tagline or phone number for easy access!
</div>
<div id="ad-300x250" class="ads"></div>
</div>
<!-- /.col-lg-12 -->
</div>
<!-- /.row -->
Javascript / jQuery代码
<script type="text/javascript">ads.push("728x90", "300x250");</script>
<script>
$(document).ready(function()
{
function loadAds()
{
$.post('ads.php', { adID: ads }, function (e) {
if (e.status == 'error')
{
$('.ads').each(function ()
{
$(this).remove();
});
}
else if (e.status == 'ok')
{
var data = e.data;
$.each(data, function (adID)
{
$('#ad-' + adID).find('.ads').html();
});
}
}, 'json');
}
loadAds();
});
</script>
JSON响应:
{ “状态”: “OK”, “数据”:{ “728x90的”:“\ r \ n \吨\ r \ n \吨\吨\ r \ n \吨\吨\吨
728x90广告&lt; / p&gt; \ r \ n \ t \ t&lt; / div&gt; \ r \ n \ t&lt; / div&gt;“,”300x250“:”\ r \ n \ t \ r \ n \ n \ t \ t \ r \ n \ t \ t \ t
300x250广告&lt; / p&gt; \ r \ n \ t \ t&lt; / div&gt; \ r \ n \ t&lt; / div&gt ;“}}
答案 0 :(得分:1)
以下内容应将数据放入正确的ID
<div class="wrapper">
<a href="#" data-url="google.com" class="myClass">Test</a>
<a href="#" data-url="google.com" class="myClass">Test</a>
<a href="#" data-url="google.com" class="myClass">Test</a>
</div>
显示的html未显示$.each(data, function (adID, adHtml){
$('#ad-' + adID).html(adHtml);
});
中ads
类<div id="ad-728x90">
的所有子级,因此find('.ads')
已被删除
如果不起作用,问题需要澄清预期结果