在两个具有相同类或数据标识的 DIV 之间插入新的 DIV。

huangapple go评论63阅读模式
英文:

Insert new DIV between two DIVs that have the same class or data-ident

问题

以下是您要翻译的内容的中文翻译:

我已经在这个问题上挣扎了一段时间。我的标记已经简化了:

    <div class=row>
        <div class="testdiv" data-ident="1"></div>
        <div class="testdiv" data-ident="2"></div>
        <div class="testdiv" data-ident="3"></div>
        <div class="testdiv" data-ident="4"></div>
        <div class="testdiv" data-ident="5"></div>
        <div class="testdiv" data-ident="6"></div>
        <div class="testdiv" data-ident="7"></div>
        <div class="testdiv" data-ident="8"></div>
        <div class="testdiv" data-ident="9"></div>
    </div>

我需要找到一种方法,在文档准备好时选择所有以下的DIV:
1. 有类名:testdiv & data-ident="XXX"
2. 它们的下一个DIV也具有类名:testdiv & data-ident="YYY"

然后,我需要在它们之间插入/附加4个新的DIV,以将它们分组,就像以下示例一样:

    <div class=row>
        <div class="NEW DIV 1"></div>
            <div class="testdiv" data-ident="1"></div>
            <div class="NEW DIV 2">
                <div class="testdiv" data-ident="2"></div>
                <div class="testdiv" data-ident="3"></div>
            </div>
            <div class="testdiv" data-ident="4"></div>
            <div class="NEW DIV 3">
                <div class="testdiv" data-ident="5"></div>
                <div class="testdiv" data-ident="6"></div>
            </div>
            <div class="testdiv" data-ident="7"></div>
        </div>
        <div class="NEW DIV 4">
            <div class="testdiv" data-ident="8"></div>
            <div class="testdiv" data-ident="9"></div>
        </div>    
    </div>

由于我无法编辑插件/源代码,我需要使用Java / jQuery来完成这个任务,但我不知道如何...
英文:

Struggling with this one for a while now. My markup simplified:

<div class=row>
    <div class="testdiv" data-ident="1"></div>
    <div class="testdiv" data-ident="2"></div>
    <div class="testdiv" data-ident="3"></div>
    <div class="testdiv" data-ident="4"></div>
    <div class="testdiv" data-ident="5"></div>
    <div class="testdiv" data-ident="6"></div>
    <div class="testdiv" data-ident="7"></div>
    <div class="testdiv" data-ident="8"></div>
    <div class="testdiv" data-ident="9"></div>
</div>

I need to find a way to select all DIVs on document ready that:

  1. has a class: testdiv & data-ident="XXX"
  2. their next DIV also has the class name: testdiv & data-ident="YYY"

Then I need to insert/append a 4 new DIV`s between them, to group them, exactly the same as the following example:

<div class=row>
    <div class="NEW DIV 1"></div>
        <div class="testdiv" data-ident="1"></div>
        <div class="NEW DIV 2">
            <div class="testdiv" data-ident="2"></div>
            <div class="testdiv" data-ident="3"></div>
        </div>
        <div class="testdiv" data-ident="4"></div>
        <div class="NEW DIV 3">
            <div class="testdiv" data-ident="5"></div>
            <div class="testdiv" data-ident="6"></div>
        </div>
        <div class="testdiv" data-ident="7"></div>
    </div>
    <div class="NEW DIV 4">
        <div class="testdiv" data-ident="8"></div>
        <div class="testdiv" data-ident="9"></div>
    </div>    
</div>

Since I can`t edit the plugin/source code, I need Java / jQuery to do this , but I don't know how to...

答案1

得分: 1

我使用以下代码来解决问题:

$(document).ready( function () {
  setTimeout(function() {
   $( '[data-ident="XX"], [data-ident="YY"]' ).wrapAll( "<div class='NEW DIV'></div>" );
  }, 2000)
});
英文:

I solve the problem using

$(document).ready( function () {
  setTimeout(function() {
   $( &#39;[data-ident=&quot;XX&quot;], [data-ident=&quot;YY&quot;]&#39; ).wrapAll( &quot;&lt;div class=&#39;NEW DIV&#39;&gt;&lt;/div&gt;&quot; );
  }, 2000)
});

huangapple
  • 本文由 发表于 2023年2月6日 20:39:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75361426.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定