最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - add a start div tag to the element and end div tag to another element - Stack Overflow

programmeradmin9浏览0评论

I have this html structure (refer below)

<div class="pagination_info"></div>
<div class="pagination_numbers"></div>

Now, what I want is to add a start div tag before the .pagination_info div and add end div tag after the .pagination_numbers div so the expected output must be (refer below)

<div class="pagination_wrapper">
    <div class="pagination_info"></div>
    <div class="pagination_numbers"></div>
</div>

what I tried so far is (refer below)

$('.pagination_info').before('<div class="pagination_wrapper">');
$('.pagination_numbers').after('</div>');

so supposedly, what im trying to achieve is to wrap the .pagination_info div and .pagination_numbers with a parent div that has a class name "pagination_wrapper" but sadly unsuccessful yet. Any help, suggestion, remendation, ideas, clues to make this work will be greatly appreciated. Thank you.

I have this html structure (refer below)

<div class="pagination_info"></div>
<div class="pagination_numbers"></div>

Now, what I want is to add a start div tag before the .pagination_info div and add end div tag after the .pagination_numbers div so the expected output must be (refer below)

<div class="pagination_wrapper">
    <div class="pagination_info"></div>
    <div class="pagination_numbers"></div>
</div>

what I tried so far is (refer below)

$('.pagination_info').before('<div class="pagination_wrapper">');
$('.pagination_numbers').after('</div>');

so supposedly, what im trying to achieve is to wrap the .pagination_info div and .pagination_numbers with a parent div that has a class name "pagination_wrapper" but sadly unsuccessful yet. Any help, suggestion, remendation, ideas, clues to make this work will be greatly appreciated. Thank you.

Share Improve this question asked Apr 27, 2015 at 8:41 Juliver GalletoJuliver Galleto 9,05727 gold badges92 silver badges169 bronze badges 1
  • 2 You're thiking in text (HTML), but that's not what you're dealing with. You're dealing with elements in a tree (the DOM). The answers below show you how to work with the elements (jQuery has a function specifically for this). – T.J. Crowder Commented Apr 27, 2015 at 8:46
Add a ment  | 

2 Answers 2

Reset to default 8

jQuery has a wrapAll method:

$(".pagination_info, .pagination_numbers").wrapAll("<div class=\"pagination_wrapper\">");

Working Example

.wrapAll() will wrap the two div's around the new div:

$(".pagination_info, .pagination_numbers").wrapAll("<div class='pagination_wrapper'>");

FIDDLE

发布评论

评论列表(0)

  1. 暂无评论