Move item after nth paragraph using jQuery

I want to move a div div #injectfrom its original position to the right after the 7th paragraph #mw-content-text.

So far I have this $('#inject').insertAfter('#mw-content-text > p:nth-child(7)');one that moves the div #injectto the right after the 7th element inside #mw-content-text, only if this 7th elite is <p>, if it doesn’t do anything else.

For example, if an article has the following structure, it works (since the 7th elite is <p>)

<div id="mw-content-text">
    <table></table>
    <h2></h2>
    <p></p>
    <p></p>
    <p></p>
    <h2></h2>
    <p></p>
    /* div #inject is injected here */
    <p></p>
    <h2></h2>
    ...

If the article looks like this, on the other hand (were there 7th elites, this <h2>), then it does nothing

<div id="mw-content-text">
    <table></table>
    <h2></h2>
    <p></p>
    <p></p>
    <h2></h2>
    <p></p>
    <h2></h2>
    <p></p>
    <p></p>
    <h2></h2>
    <p></p>
    ...

- , ? 7- <p> # mv-content-text 7- , a <p>?

+4
2

p:nth-of-type(7) p:nth-child(7).

:

  • p:nth-child(7) , <p>, th
  • p:nth-of-type(7) , 7 th<p>
+2

eq. :

$('#inject').insertAfter($('#mw-content-text>p').eq(6));

jsfiddle: https://jsfiddle.net/qkjws7jv/

, eq , 6

+1

Source: https://habr.com/ru/post/1620823/


All Articles