Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 如何用div替换第五篇文章?_Jquery_Html_Tumblr - Fatal编程技术网

Jquery 如何用div替换第五篇文章?

Jquery 如何用div替换第五篇文章?,jquery,html,tumblr,Jquery,Html,Tumblr,我正在处理这个tumblr主题()我因为tumblr会在文章中自动生成帖子,要在其中一些文章之间添加广告,我只想用div替换每5篇文章,我将在其中添加300x250广告 我所说的部分的Html: <div class="content"> <article></article> <article></article> <article></article> <article></

我正在处理这个tumblr主题()我因为tumblr会在文章中自动生成帖子,要在其中一些文章之间添加广告,我只想用div替换每5篇文章,我将在其中添加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)')。替换为('.ad');

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

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


$('article:nth child(5n)')。替换为('.ad')。text();//删除术语n
或
$('article:nth child(5n)')。替换为('.ad').html();正如博詹德拉所提到的

我不明白你为什么把
n
放在
n个孩子(5n)
里。删除它,它没有任何意义。

您尝试的代码的具体问题是什么?是否要替换第五篇文章或在每五篇文章后插入广告?是否要用广告替换实际内容?或者你想在第五和第六个
元素之间插入广告?只要广告最终出现在第五篇文章现在所在的位置,这并不重要。但现在似乎没有发生。。。我会尝试使用window.load,可能是因为文章需要一点时间才能加载(?)可能是@mikeddt的副本。如果使用15篇文章的正常tumblr布局,效果会很好(这是tumblr在没有任何调整的情况下可以接受的最多的布局)但是如果你使用无限卷轴,它会生成一百万个帖子,所以手动在每5篇帖子之后添加一个广告是不可能的,因为我每添加5篇帖子就必须添加一个新的广告。谢谢!这对我有用!它看起来仍然很奇怪,但只要文章克隆并添加到每5篇文章中,就可以通过css轻松修复。再次感谢@理查德伦德维瑟:很高兴能帮上忙:)很抱歉再次打扰你,但当我尝试应用保证金等时,它现在不会应用它。我试图通过Chrome检查元素,看看问题出在哪里,它的右边似乎有无限的边距,这不能通过添加右边的边距来消除:0;(甚至不带!重要)。有什么想法吗?@RichardLundWinther:我不擅长css。发布带有相关代码和css的新问题。有人一定会帮你的。谢天谢地,我刚刚解决了这个问题,所以也不需要这样做,谢谢。我添加了n,因为这意味着我将它应用于每5篇文章5次n 5,10 15等。好的,所以广告必须出现在每5个街区,而不仅仅是第5街区。问题已经解决了,不用担心,谢谢:)
<script>    
$('article:nth-child(5n)').replaceWith('.ad').text();   // remove the term n
or
$('article:nth-child(5n)').replaceWith('.ad').html();   as Bhojendra mentioned
</script>