Question

Html code

<div class="ar">
    <div class="IntroText">
        xxxxxxxxxxx
        <span class="youtube">
        <object =xxxxxxxxxxxxxxxxxxxxxxxx>
        </object>
        </span>
        xxxxxxxxxxx
        xxxxx
    </div>
</div>
<div class="ar">
    <div class="IntroText">
        xxxxxxxxxxx
        xxxxxxxxxxx
        xxxxx
        </div>
</div>
<div class="ar">
    <div class="IntroText">
        xxxxxxxxxxx
        <span class="youtube">
        <object =xxxxxxxxxxxxxxxxxxxxxxxx>
        </object>
        </span>
        <span class="youtube">
        <object =xxxxxxxxxxxxxxxxxxxxxxxx>
        </object>
        </span>
        xxxxxxxxxxx
        xxxxx
        </div>
</div>

How do I use jQuery each function prepend the first span.youtube element to class ar? or appendTo? I try use jquery each but all span.youtube element will go the same

$(.IntroText).find(span.youtube).each(function(){$(this).appendTo('.IntroText')}
Était-ce utile?

La solution

If you want to move first span.youtube element then you have to iterate through .IntroText elements and find first span.youtube like this;

$('.IntroText').each(function (i) {
    var $this = $(this);
    $this.before($this.find('span.youtube:first'));
});

This moves first span.youtube element before each .IntroText element. You can use .after() too if it's what you need.

Here is a jsFiddle example that you can see the markup after modification.

Autres conseils

$(function(){
$('.IntroText').find('span.youtube').each(function(){$('<p>prepend sometext</p>').prependTo(this)});
$('.IntroText').find('span.youtube').each(function(){$('<p>append sometext</p>').appendTo(this)});    
});

http://jsfiddle.net/SXBwh/1/

This is very clunky but it looks like it's working:

http://jsfiddle.net/lharby/ZSdXS/

$('.IntroText span.youtube').each(function(){
   $('.IntroText').append(this);
});

Is that what you are looking for?

You need to wrap html elements, id's and classes in single or double quotes inside the function.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top