2015-01-30 3 views
0

제 n 번째 기사마다 div를 복제하려고하는 사이트에 문제가 있습니다. (http://trollfutbol.tumblr.com/).clone이 내 div를 복사하지 않는 이유는 무엇입니까?

모든 7 번째 기사에 adWrap div를 추가하는 스크립트를 추가했습니다.

$('article:nth-child(7n)').replaceWith('.adWrap').clone(); 

그리고 그것을 복제 수행하고 7에 광고를 추가하지만 광고가 14 기사 등 그것은 단지 포장을 추가로 자신을 다시 추가하지 않습니다.

아이디어가 있으십니까?

은 HTML은 기본적으로

<div class="content"> 
    <article></article> 
    <article></article> 
    <article></article> 
    <article></article> 
    <article></article>   regenerating infinite times (as many posts I will have) 
    <article></article> 
    <article></article> 
    <article></article> 
    <article></article> 
    <div class="adWrap">ad iframe</div> 
</div> 
+0

['.clone()'] (https://api.jquery.com/clone/)은 DOM을 수정하지 않습니다. 그 외에도'.clone()'은''.replaceWith ('.adWrap')'] (https://api.jquery.com/replacewith/)에 의해 반환되는 요소에 대해 호출됩니다. .] 제거 된 요소 집합을 반환한다. –

+0

아마 $ ('article : nth-child (7n)')을 찾고있을 것이다. replaceWith (function() {return $ ('.adWrap').();});' – adeneo

+0

'

'요소에 대한 샘플 마크 업을 게시 할 수 있습니까? 디버깅에 도움이 될 것입니다. – Kita

답변

2

작은 실수 - replaceWith 명령과 변수 문자에 괄호를 놓친 - 달러. (0) 나는 그나마 replaceWith 여러 요소가있을 때 작동 방법을 알고 EQ :

올바른 JS 코드는 당신이 또한 추가 할 수 있습니다 여기에 $과 괄호를 추가 할 필요가

$('article:nth-child(7n)').replaceWith($('.adWrap').clone()); 

http://jsfiddle.net/kbqcr9vw/2/

0

입니다 찾음

$('article:nth-child(7n)').replaceWith($('.adWrap:eq(0)').clone()); 
관련 문제