我看了高处和低处,想不通这个!
在一个页面上,我有具有特定类别 (plmore) 的链接。在同一页上,我有具有特定类 (fcontainer) 等的 div。使用 plmore 类的链接数将始终等于使用 fcontainer 类的 div 数。
我的问题:
我需要用使用 plmore 找到的链接包装具有 fcontainer
类的 div
。
伪代码:
获取 HREFS 数组
获取 DIV ID 数组
用 HREFS 包装 DIVS
这是我目前所拥有的:
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
jQuery(document).ready(function($) {
var hrefs = new Array();
$('a.plmore').each(function() {
hrefs.push($(this).find('a').attr('href'));
});
var features = new Array();
$('fcontainer').each(function() {
features.push($(this).find('div').attr('id'));
});
/* how does one pop from both arrays and wrap?? */
});
</script>
最佳答案
你是说喜欢
jQuery(function ($) {
//find all the target anchor elements
var $as = $('a.plmore');
//find the div elements
$('.fcontainer').each(function (idx) {
//wrap the div at index idx using the href value of anchor element at index idx
$(this).wrap($('<a/>', {
href: $as.eq(idx).attr('href')
}))
});
});
演示:Fiddle
关于javascript - 如何在 jQuery 中推送、弹出和包装值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20475428/