jQuery 用 div 包装多个 id [英] jQuery wrap multiple ids with div

查看:33
本文介绍了jQuery 用 div 包装多个 id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用一个 div 包装选定的 id 集合,并用不同的一个包装其他集合,但不太确定它应该如何工作.

我有以下 html 代码:

<div id="block_1"><div class="post">

<div id="block_2"><div class="post">

<div id="block_3"><div class="post">

<div id="block_4"><div class="post">

<div id="block_5"><div class="post">

并且想用Jquery做两个不同的set用另一个div包裹,像这样:

<div class="wrapright"><div id="block_1"><div class="post">

<div id="block_4"><div class="post">

<div id="block_5"><div class="post">

<div class="wrapleft"><div id="block_2"><div class="post"></div>

<div id="block_3"><div class="post"></div>

如你所见,我想用 class="wrapright" 包裹 block 1,4,5 并用 class="包裹块 2,3wrapleft"

有没有 Jquery 方法可以做到这一点!!我找到了一些方法,但都是针对单个集合的,而不是多个...

感谢您的帮助!

解决方案

您可以使用 .wrapAll().

如果 ids 和元素集是常量,那么你可以

$('#block_1, #block_4, #block_5').wrapAll('<div class="wrapright"/>');$('#block_2, #block_3').wrapAll('<div class="wrapleft"/>');

演示:小提琴

<小时>

如果元素集不是恒定的,那么你需要想出一个关于如何确定组的逻辑.

I was trying to wrap selected sets of ids with a div and wrap other sets with different one, but not quite sure how it should work.

I have the following html code:

<div class="wrapper">
<div id="block_1">
        <div class="post">
        </div>
</div>
<div id="block_2">
        <div class="post">
        </div>
</div>
<div id="block_3">
        <div class="post">
        </div>
</div>
<div id="block_4">
        <div class="post">
        </div>
</div>
<div id="block_5">
        <div class="post">
        </div>
</div>

And want to use Jquery to make two different of sets wrapped with another div, like this:

<div class="wrapper">
<div class="wrapright">
    <div id="block_1">
            <div class="post">
            </div>
    </div>
    <div id="block_4">
            <div class="post">
            </div>
    </div>
    <div id="block_5">
            <div class="post">
            </div>
    </div>
</div>
<div class="wrapleft">
    <div id="block_2">
            <div class="post"></div>
    </div>

    <div id="block_3">
            <div class="post"></div>
    </div>

    </div>

</div>

As you see, I want to wrap block 1,4,5 with class="wrapright" and wrap block 2,3 with class="wrapleft"

is there a Jquery way that I can do this!! I found some ways, but all for single set, not multiple...

your help is appreciated!

解决方案

You can use .wrapAll().

If the ids and element set are constant then you can just

$('#block_1, #block_4, #block_5').wrapAll('<div class="wrapright" />');
$('#block_2, #block_3').wrapAll('<div class="wrapleft" />');

Demo: Fiddle


If the element sets are not constant then you need to come up with a logic on how to determine the groups.

这篇关于jQuery 用 div 包装多个 id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
前端开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆