如何在jQuery中推送,弹出和包装值?

问题描述 投票:3回答:1

我看起来又高又低,无法想出这一个!

在页面上,我有一个具有某个类(plmore)的链接。在同一页面上,我有一些div,其中包含某个类(fcontainer)。类plmore的链接数总是等于使用fcontainer类的div数。

我的问题:

我需要包含div类的fcontainers和使用plmore找到的链接。

伪代码: 得到HREFS的阵列 得到DIV IDS的阵列 带有HREFS的WRAP 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>
javascript jquery arrays push pop
1个回答
3
投票

你的意思是

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')
        }))
    });
});

但是:ぁzxswい

© www.soinside.com 2019 - 2024. All rights reserved.