jquery - jQuery:有时随机失败
问题描述
我有一个博客,在每篇文章的底部随机宣传 x3 篇文章。有时,此功能无法显示全部 3 个(缺少 1 个)。
我看不出问题出在哪里。
jQuery
$.get("https://www.citychurchchristchurch.co.nz/blog.php", function(data) {
var $quotes = $(data).find(".container-articles-5"),
count = $quotes.length,
$random = function() {
return $quotes.eq(Math.floor(Math.random() * count));
};
$(".blog-ad-1").append($random);
});
HTML(显示广告的地方)
<aside class="blog-ad-4">
<div class="blog-ad-5">
<div class="blog-ad-3">
<div class="blog-ad-1"></div>
</div>
<div class="blog-ad-3">
<div class="blog-ad-1"></div>
</div>
<div class="blog-ad-3">
<div class="blog-ad-1"></div>
</div>
</div>
</aside>
应显示所有 3 个广告。在此处查看现场演示(靠近底部):https ://www.citychurchchristchurch.co.nz/blog/eo-cycling-20181030
解决方案
这只是一个猜测,但我会尝试以下方法:
$.get("https://www.citychurchchristchurch.co.nz/blog.php", function(data) {
var $quotes = $(data).find(".container-articles-5"),
count = $quotes.length,
$random = function() {
var articleNumber = Math.floor(Math.random() * count);
console.log(articleNumber);
return $quotes.eq(articleNumber);
}
$(".blog-ad-1").append($random);
});
通过这种方式,您可以检查是否总是在生成的同一 articleNumber(例如 0)上显示广告失败。
推荐阅读
- php - 如何防止访问 Apache2 中的文件夹并允许访问 PHP?
- tensorflow - 每个目标具有多个输入序列的 RNN
- php - Magento 1.9 从供应商资料(市场)到订单视图(在管理员中)的附加信息
- java - 哪一个是 Java 中的 Set 和 Maps 之间最快的?
- ios - 使用 iPhone 作为网关的物联网设备
- python - Tensorflow 上 Proximal AdaGrad 优化器的正确关键字是什么?
- spring-boot - 在 Quartz_job_schedule 表上的 Quartz 调度程序中找不到分页支持
- mqtt - Mqtt-Kafka 连接器认证
- python - Runtime error when trying to run a Django app
- python - numba 中 datetime.datetime.utcfromtimestamp(dateFrom) 的解决方法?