开发者

How to wrapAll elements like this

<div id="editor">
  text
  <div id="wrap"> <!-- here -->
    <img src="images/1.jpg"/>
  </div>
  text</p>
  <div id="wrap"> <!-- and here -->
    <im开发者_StackOverflowg src="images/3.jpg"/>
    <img src="images/4.jpg"/>
    <img src="images/5.jpg"/>
    <img src="images/6.jpg"/>
  </div>
</div>


I imagine you want to wrap groupings of <img> elements.

If so, do this:

Example: http://jsfiddle.net/patrick_dw/RXFVM/1/

$('#editor > img:not(img + img)').each(function() {
    $(this).nextUntil(':not(img)').andSelf().wrapAll('<div class="wrap"></div>');
});
  • 'img:not(img + img)': get images that are not preceded by an image
  • .each(): iterate over them
  • .nextUntil(':not(img)'): get all next elements, as long as they are images
  • .andSelf(): include the original image
  • .wrapAll('<div class="wrap"></div>'): wrap the group

Note that I changed id="wrap" to class="wrap" since you can't reuse IDs on a page.


EDIT: Updated example link to use non-broken images.


as i understand it you want to wrap images with jquery?

well it could look like this

$('img').wrap('<div id="wrap" />');

but really you have to be more precise if you want a better answer

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜