javascript - jQuery each() not working correctly with wrapAll() and wrap()
问题描述
I've created a simple triptych. The content is entered on a CMS so I have have to wrap the divs with JS. The issue is that when I wrap the contents of 2 triptychs everything is wrapped into one triptych. Am i using jQuery's each()
function correctly? Or what am I doing wrong? Thanks!
$(document).ready(function () {
$(".triptych").each(function () {
$('.field--name-field-topic-1-icon, .field--name-field-topic-1-title').wrapAll('<div class="iconContainer iconContainer1"> </div>');
$('.field--name-field-topic-2-icon, .field--name-field-topic-2-title').wrapAll('<div class="iconContainer iconContainer2"></div>');
$('.field--name-field-topic-3-icon, .field--name-field-topic-3-title').wrapAll('<div class="iconContainer iconContainer3"></div>');
$('.iconContainer').wrapAll('<div class="iconsContainer"> </div>');
$('.iconContainer1').wrap('<a role="button" class="topic1-filter tab" data-tabid="topic1"></a>');
$('.iconContainer2').wrap('<a role="button" class="topic2-filter tab" data-tabid="topic2"></a>');
$('.iconContainer3').wrap('<a role="button" class="topic3-filter tab" data-tabid="topic3"></a>');
$('.field--name-field-topic-1-content, .field--name-field-topic-2-content, .field--name-field-topic-3-content').wrapAll('<div class="tripctychContent-container"></div>');
});
});
.field--name-field-topic-2-content,.field--name-field-topic-3-content,.field--name-field-topic-1-content {
display: none;
}
.showTopicContent {
display: block;
}
.hideMoreText {
display: none;
}
.iconsContainer {display: flex;justify-content: space-between;}
p{text-align: center;}
.iconsContainer a {flex: 1; margin-right: 5px;}
.triptych img { width: 150px;margin: 0 auto;display: block;height: 150px;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="triptych">
<div class="inner">
<div class="field--name-field-topic-1-icon"><img src="https://fakeimg.pl/300/" ></div>
<div class="field--name-field-topic-1-title field--type-text-long"><p>Topic 1 title</p></div>
<div class="field--name-field-topic-2-icon"><img src="https://fakeimg.pl/300/" ></div>
<div class="field--name-field-topic-2-title field--type-text-long"><p>Topic 2 title</p></div>
<div class="field--name-field-topic-3-icon"> <img src="https://fakeimg.pl/300/" ></div>
<div class="field--name-field-topic-3-title field--type-text-long"><p>Topic 3 title</p></div>
<div class="field--name-field-topic-1-content"><p>Topic 1 content...</p></div>
<div class="field--name-field-topic-2-content"><p>Topic 2 content...</p></div>
<div class="field--name-field-topic-3-content"><p>Topic 3 content...</p></div>
</div>
</div>
<div>
<h1>this is a random div that should be between the 2 tripticks</h1>
</div>
<div class="triptych">
<div class="inner">
<div class="field--name-field-topic-1-icon"><img src="https://fakeimg.pl/300/" ></div>
<div class="field--name-field-topic-1-title field--type-text-long"><p>Triptych 2 Topic 1 title</p></div>
<div class="field--name-field-topic-2-icon"><img src="https://fakeimg.pl/300/" ></div>
<div class="field--name-field-topic-2-title field--type-text-long"><p>Triptych 2 Topic 2 title</p></div>
<div class="field--name-field-topic-3-icon"> <img src="https://fakeimg.pl/300/" ></div>
<div class="field--name-field-topic-3-title field--type-text-long"><p>Triptych 2 Topic 3 title</p></div>
<div class="field--name-field-topic-1-content"><p>Triptych 2 Topic 1 content...</p></div>
<div class="field--name-field-topic-2-content"><p>Triptych 2Topic 2 content...</p></div>
<div class="field--name-field-topic-3-content"><p>Triptych 2 Topic 3 content...</p></div>
</div>
</div>
解决方案
You have to use this
to find the specific element inside of the class:
$(document).ready(function () {
$(".triptych").each(function () {
$(this).find('.field--name-field-topic-1-icon, .field--name-field-topic-1-title').wrapAll('<div class="iconContainer iconContainer1"> </div>');
$(this).find('.field--name-field-topic-2-icon, .field--name-field-topic-2-title').wrapAll('<div class="iconContainer iconContainer2"></div>');
$(this).find('.field--name-field-topic-3-icon, .field--name-field-topic-3-title').wrapAll('<div class="iconContainer iconContainer3"></div>');
$(this).find('.iconContainer').wrapAll('<div class="iconsContainer"> </div>');
$(this).find('.iconContainer1').wrap('<a role="button" class="topic1-filter tab" data-tabid="topic1"></a>');
$(this).find('.iconContainer2').wrap('<a role="button" class="topic2-filter tab" data-tabid="topic2"></a>');
$(this).find('.iconContainer3').wrap('<a role="button" class="topic3-filter tab" data-tabid="topic3"></a>');
$(this).find('.field--name-field-topic-1-content, .field--name-field-topic-2-content, .field--name-field-topic-3-content').wrapAll('<div class="tripctychContent-container"></div>');
});
});
.field--name-field-topic-2-content,.field--name-field-topic-3-content,.field--name-field-topic-1-content {
display: none;
}
.showTopicContent {
display: block;
}
.hideMoreText {
display: none;
}
.iconsContainer {display: flex;justify-content: space-between;}
p{text-align: center;}
.iconsContainer a {flex: 1; margin-right: 5px;}
.triptych img { width: 150px;margin: 0 auto;display: block;height: 150px;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="triptych">
<div class="inner">
<div class="field--name-field-topic-1-icon"><img src="https://fakeimg.pl/300/" ></div>
<div class="field--name-field-topic-1-title field--type-text-long"><p>Topic 1 title</p></div>
<div class="field--name-field-topic-2-icon"><img src="https://fakeimg.pl/300/" ></div>
<div class="field--name-field-topic-2-title field--type-text-long"><p>Topic 2 title</p></div>
<div class="field--name-field-topic-3-icon"> <img src="https://fakeimg.pl/300/" ></div>
<div class="field--name-field-topic-3-title field--type-text-long"><p>Topic 3 title</p></div>
<div class="field--name-field-topic-1-content"><p>Topic 1 content...</p></div>
<div class="field--name-field-topic-2-content"><p>Topic 2 content...</p></div>
<div class="field--name-field-topic-3-content"><p>Topic 3 content...</p></div>
</div>
</div>
<div>
<h1>this is a random div that should be between the 2 tripticks</h1>
</div>
<div class="triptych">
<div class="inner">
<div class="field--name-field-topic-1-icon"><img src="https://fakeimg.pl/300/" ></div>
<div class="field--name-field-topic-1-title field--type-text-long"><p>Triptych 2 Topic 1 title</p></div>
<div class="field--name-field-topic-2-icon"><img src="https://fakeimg.pl/300/" ></div>
<div class="field--name-field-topic-2-title field--type-text-long"><p>Triptych 2 Topic 2 title</p></div>
<div class="field--name-field-topic-3-icon"> <img src="https://fakeimg.pl/300/" ></div>
<div class="field--name-field-topic-3-title field--type-text-long"><p>Triptych 2 Topic 3 title</p></div>
<div class="field--name-field-topic-1-content"><p>Triptych 2 Topic 1 content...</p></div>
<div class="field--name-field-topic-2-content"><p>Triptych 2Topic 2 content...</p></div>
<div class="field--name-field-topic-3-content"><p>Triptych 2 Topic 3 content...</p></div>
</div>
</div>
推荐阅读
- c# - 使用 IPC 中断正在运行的进程
- python - Pandas Pivot 表返回空数据框
- javascript - TypeError:无法读取 discord.js 中未定义的属性“成员”
- r - 在 R 中按 ID 对日期进行分类
- python - 当触发机器人响应的人删除他们的初始消息时删除机器人的响应
- javascript - 以编程方式将样式添加到 D3 热图的列
- javascript - HTML 页面渲染 - 通过 Javascript 调整大小进行的更正感觉有点刺耳
- python-3.x - 搜索多个字符串
- r - model.frame.default(Terms, newdata, na.action = na.action, xlev = object$xlevels) 中的错误:
- react-native - 导入文件后,测验应用程序不会加载