英文:
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:
- has a class: testdiv & data-ident="XXX"
- 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() {
$( '[data-ident="XX"], [data-ident="YY"]' ).wrapAll( "<div class='NEW DIV'></div>" );
}, 2000)
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论