LeTung
LeTung

Reputation: 123

Using jquery ".after" and ".before"?

The first to thank everyone for helping me. Sorry for my english is bad :)

I need jquery use .after and .before (or maybe one other code) to add div to the img, and to run it only acts on class (swiper) and it does not act on the class structured like it.

For example:

<div class="wrapper">
    <img src="..1.jpg">
    <img src="..2.jpg">
    <img src="..3.jpg">
</div>
<div class="swiper">
    <img src="..1.jpg">
    <img src="..2.jpg">
    <img src="..3.jpg">
</div>

And Result:

<div class="wrapper">
    <img src="..1.jpg">
    <img src="..2.jpg">
    <img src="..3.jpg">
</div>
<div class="swiper">
    <div class="slide">
        <img src="..1.jpg">
    </div>
    <div class="slide">
        <img src="..2.jpg">
    </div>
    <div class="slide">
        <img src="..3.jpg">
    </div>
</div>

Upvotes: 0

Views: 52

Answers (2)

DinoMyte
DinoMyte

Reputation: 8868

Try this.

$('.swiper img').each(function(){
    $(this).nextUntil('img') // iterate until next 'img' tag
    .addBack() // include the current 'img' tag
    .wrapAll('<div class="slide">'); // wrap with the 'div'   
});

http://jsfiddle.net/eqntjb01/2/

Upvotes: 1

Taplar
Taplar

Reputation: 24965

From your example what it looks like your really after is wrap().

http://api.jquery.com/wrap/

This function will wrap matched elements with whatever element you want. In this case wrapping images with a div of class slide.

Possibly something like

$('.swiper img').wrap('<div class="slide">');

Upvotes: 1

Related Questions