0
<div class="Label" id="Label1">
<h2>Menu</h2>
<ul>
<li>
<a dir="ltr" href="#">Sub</a>
</li>
<li>
<a dir="ltr" href="#">Sub2</a>
</li>
<li>
<a dir="ltr" href="#">Sub3</a>
</li>
</ul>
<div>
...

和子菜单:

<div class="Label" id="Label2">
<h2>Sub</h2>
<ul>
<li>
<a dir="ltr" href="#">Sub-child-1</a>
</li>
<li>
<a dir="ltr" href="#">Sub-child-2</a>
</li>
</ul>
<div>

<div class="Label" id="Label3">
<h2>Sub2</h2>
<ul>
<li>
<a dir="ltr" href="#">Sub2-child-1</a>
</li>
<li>
<a dir="ltr" href="#">Sub2-child-2</a>
</li>
</ul>
<div>

....

我想要一个将 $('.Label h2').html() 匹配与 $('.Label a').html() 进行比较的代码,然后将“包含匹配的'div'”移动到“ 'a' 匹配。

$('.Label a').each(function(){
  if( $(this).html() == $('.Label h2').html() ){ 
      $(this).parent('li').append('$('.Label h2').parent('div')')
  };
});

不行@@请帮帮我!

4

1 回答 1

0

此代码将为您完成这项工作。但由于您页面的 css,它看起来像一团糟。你应该修改你的CSS。我已将 SubLabel 类添加到移动的一个(子菜单)中。

$('.Label a').each(function(){
  var that = this;
  $('.Label h2').each(function(){
      if($(that).html() === $(this).html()){
         $(that).append($(this).parent().addClass('SubLabel'));
      }
  });
});
于 2013-10-02T14:46:06.637 回答