0

我在循环js中有问题。我想将data-icon值从.navclass 复制到 liin #item-control。有没有简单的方法来用jquery解决它?

<div id="nav-container">
  <ul class="nav">
   <li data-icon="images/1.png">Item 1</li>
   <li data-icon="images/2.png">Item 2</li>
   <li data-icon="images/3.png">Item 3</li>
  </ul>
</div>

<div id="item-control">
  <li>Item Control 1</li>
  <li>Item Control 2</li>
  <li>Item Control 3</li>
</div>

我试过的:

$(document).ready(function () {
    var a = 1;
    $('#container #nav-container ul.nav li').each(function () {
        var item_control = $(this).parent().parent().parent().find('#item-control li');
        item_control.addClass('item' + a);  //this is what I tried before adding data-icon attributes to #item-control li
        a++;
    });
});
4

1 回答 1

0
var $navItems = $('.nav li'),
    $itemControlItems = $('#item-control li');
$.each($navItems, function(index, el) {
    $($itemControlItems[index]).data('icon', $(el).data('icon'));
});

小提琴

于 2013-01-19T04:31:46.840 回答