jquery - 如何用div替换每5篇文章?

标签 jquery html tumblr

我正在研究这个 tumblr 主题 ( http://trollfutbol.tumblr.com/ ),因为 tumblr 会在文章中自动生成帖子,为了在其中一些文章之间添加广告,我只想将每 5 篇文章替换为我将在其中添加的 div 300x250 广告。

我正在谈论的部分的 Html:

<div class="content">
  <article></article>
  <article></article>
  <article></article>
  <article></article>
  <article></article>           Automically generating as many posts as I have.
  <article></article>
  <article></article>
  <article></article>
  <article></article>
   </div>
   <div class="ad">
   </div>

我尝试这样做:

<script>    
$('article:nth-child(5n)').replaceWith('.ad');
</script>

最佳答案

您需要克隆元素以附加到多个元素:

$('article:nth-child(5n)').replaceWith($('.ad').clone());

<强>

Working Demo

关于jquery - 如何用div替换每5篇文章?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28242278/

相关文章:

php - 我需要对 jquery 中选择框值的值求和

python - Tumblr API 发布到辅助博客

html - 我的网站似乎以非常 "patchy"的方式加载。有任何解决这个问题的方法吗?

html - 中心导航按钮

html - 如何在 Tumblr 中嵌入 HTML 代码

javascript - Jquery $.post脚本运行3次(多次INSERT)

javascript - Rails 没有处理我的 jQuery .click 事件(甚至无法弹出警报。)

jquery - 在图像上放置文本

javascript - 尝试使用 ng-style 访问 HTML 中的作用域变量

javascript - 设置窗口标题栏的颜色 - electron.js