2014-05-01 31 views
0

我试了一整天,就像我需要重新整理此菜单包,但我是新来的jQuery .. 我所试图做的是:jQuery的提取内容,开关里面的内容,在新的标签

  • 从这个菜单的最后td.top里面有3个ul.sub li,我希望每个都从那里提取,它的内容放在td.top中。

  • 然后里面的内容有一个div和一个链接,我需要切换位置,链接必须是DIV之前(就像其他td.top内)

  • 基本上,我尽量让3 ul.sub。 li相同(添加相同的类,具有相同的结构等,如td.top的其余部分)

我该怎么做? 我明白任何提示..

这是我迄今为止所使用的代码:

$("ul.sub a.more_top_element").removeClass('button more_top_element').addClass("top_link").removeAttr('style').removeAttr('value'); 
$("ul.sub div").removeClass('more_sub').addClass("sub"); 

..我在这里应该有代码来提取3 ul.sub李里面的内容,并切换链接与格,然后把它包在td.top ..

这是我需要jQuery来修改HTML:

<table class="topMenu" cellpadding="0" cellspacing="0"> 
<tbody> 
<tr> 
    <td class="top"> 
     <a href="http://localhost/d7t/m/photos/home/" class="top_link"><span class="down bx-def-padding-sec-leftright">Photos</span> 
     <!--[if gte IE 7]><!--> 
     </a> 
     <!--<![endif]--> 
     <div class="sub"> 
      <!--[if lte IE 6]><table id="mmm"><tr><td><![endif]--> 
      <ul class="sub main_elements"> 
       <li> 
       <a href="http://localhost/d7t/m/photos/home/" target="_self" class="button more_ntop_element">Home</a> 
       </li> 
       <li> 
       <a href="http://localhost/d7t/m/photos/albums/browse/all" target="_self" class="button more_ntop_element">Albums</a> 
       </li> 
       <li> 
       <a href="http://localhost/d7t/m/photos/browse/all" target="_self" class="button more_ntop_element">Recent</a> 
       </li> 
       <li> 
       <a href="http://localhost/d7t/m/photos/browse/top" target="_self" class="button more_ntop_element">Top</a> 
       </li> 
       <li> 
       <a href="http://localhost/d7t/m/photos/browse/popular" target="_self" class="button more_ntop_element">Popular</a> 
       </li> 
       <li> 
       <a href="http://localhost/d7t/m/photos/browse/featured" target="_self" class="button more_ntop_element">Featured</a> 
       </li> 
       <li> 
       <a href="http://localhost/d7t/m/photos/tags" target="_self" class="button more_ntop_element">Tags</a> 
       </li> 
       <li> 
       <a href="http://localhost/d7t/m/photos/categories" target="_self" class="button more_ntop_element">Categories</a> 
       </li> 
       <li> 
       <a href="http://localhost/d7t/m/photos/rate" target="_self" class="button more_ntop_element">Rater</a> 
       </li> 
       <li> 
       <a href="http://localhost/d7t/m/photos/calendar" target="_self" class="button more_ntop_element">Calendar</a> 
       </li> 
       <li> 
       <a href="http://localhost/d7t/searchKeyword.php?type=bx_photos" target="_self" class="button more_ntop_element">Search</a> 
       </li> 
      </ul> 
      <!--[if lte IE 6]></td></tr></table></a><![endif]--> 
     </div> 
    </td> 
    <td class="top"> 
     <a href="http://localhost/d7t/m/poll/&amp;action=poll_home" class="top_link"><span class="down bx-def-padding-sec-leftright">Polls</span> 
     <!--[if gte IE 7]><!--> 
     </a> 
     <!--<![endif]--> 
     <div class="sub"> 
      <!--[if lte IE 6]><table id="mmm"><tr><td><![endif]--> 
      <ul class="sub main_elements"> 
       <li> 
       <a href="http://localhost/d7t/m/poll/&amp;action=poll_home" target="_self" class="button more_ntop_element">Polls Home</a> 
       </li> 
       <li> 
       <a href="http://localhost/d7t/m/poll/" target="_self" class="button more_ntop_element">All Polls</a> 
       </li> 
       <li> 
       <a href="http://localhost/d7t/m/poll/&amp;action=popular" target="_self" class="button more_ntop_element">Popular</a> 
       </li> 
       <li> 
       <a href="http://localhost/d7t/m/poll/&amp;action=featured" target="_self" class="button more_ntop_element">Featured</a> 
       </li> 
       <li> 
       <a href="http://localhost/d7t/m/poll/calendar" target="_self" class="button more_ntop_element">Calendar</a> 
       </li> 
       <li> 
       <a href="http://localhost/d7t/searchKeyword.php?type=poll" target="_self" class="button more_ntop_element">Search</a> 
       </li> 
       <li> 
       <a href="http://localhost/d7t/m/poll/tags" target="_self" class="button more_ntop_element">Tags</a> 
       </li> 
       <li> 
       <a href="http://localhost/d7t/m/poll/categories" target="_self" class="button more_ntop_element">Categories</a> 
       </li> 
      </ul> 
      <!--[if lte IE 6]></td></tr></table></a><![endif]--> 
     </div> 
    </td> 
    <td class="top"> 
     <a href="javascript: void(0);" onclick="void(0);" class="top_link"> 
     <span class="down bx-def-padding-sec-leftright">More</span> 
     <!--[if gte IE 7]><!--> 
     </a> 
     <!--<![endif]--> 
     <!--[if lte IE 6]><table id="mmm"><tr><td><![endif]--> 
     <div style="position:relative;display:block;"> 
      <ul class="sub"> 
       <li class=""> 
       <div class="sub"> 
        <ul class="sub main_elements"> 
         <li> 
         <a href="http://localhost/d7t/m/sites/home/" target="_self" class="button more_ntop_element">Sites Home</a> 
         </li> 
         <li> 
         <a href="http://localhost/d7t/m/sites/browse/all" target="_self" class="button more_ntop_element">All Sites</a> 
         </li> 
         <li> 
         <a href="http://localhost/d7t/m/sites/browse/admin" target="_self" class="button more_ntop_element">Admin Sites</a> 
         </li> 
         <li> 
         <a href="http://localhost/d7t/m/sites/browse/users" target="_self" class="button more_ntop_element">User Sites</a> 
         </li> 
         <li> 
         <a href="http://localhost/d7t/m/sites/browse/top" target="_self" class="button more_ntop_element">Top Rated</a> 
         </li> 
         <li> 
         <a href="http://localhost/d7t/m/sites/browse/popular" target="_self" class="button more_ntop_element">Popular</a> 
         </li> 
         <li> 
         <a href="http://localhost/d7t/m/sites/browse/featured" target="_self" class="button more_ntop_element">Featured</a> 
         </li> 
         <li> 
         <a href="http://localhost/d7t/m/sites/tags" target="_self" class="button more_ntop_element">Tags</a> 
         </li> 
         <li> 
         <a href="http://localhost/d7t/m/sites/categories" target="_self" class="button more_ntop_element">Categories</a> 
         </li> 
         <li> 
         <a href="http://localhost/d7t/m/sites/calendar" target="_self" class="button more_ntop_element">Calendar</a> 
         </li> 
         <li> 
         <a href="http://localhost/d7t/m/sites/search" target="_self" class="button more_ntop_element">Search</a> 
         </li> 
        </ul> 
       </div> 
       <a href="http://localhost/d7t/m/sites/home/" class="top_link">Sites</a> 
       <div class="clear_both sub"> 
       </div> 
       </li> 
       <li class=""> 
       <div class="sub"> 
        <ul class="sub main_elements"> 
         <li> 
         <a href="http://localhost/d7t/m/sounds/home/" target="_self" class="button more_ntop_element">Sounds home</a> 
         </li> 
         <li> 
         <a href="http://localhost/d7t/m/sounds/albums/browse/all" target="_self" class="button more_ntop_element">Sound albums</a> 
         </li> 
         <li> 
         <a href="http://localhost/d7t/m/sounds/browse/all" target="_self" class="button more_ntop_element">All Sounds</a> 
         </li> 
         <li> 
         <a href="http://localhost/d7t/m/sounds/browse/top" target="_self" class="button more_ntop_element">Top Sounds</a> 
         </li> 
         <li> 
         <a href="http://localhost/d7t/m/sounds/browse/popular" target="_self" class="button more_ntop_element">Popular Sounds</a> 
         </li> 
         <li> 
         <a href="http://localhost/d7t/m/sounds/browse/featured" target="_self" class="button more_ntop_element">Featured Sounds</a> 
         </li> 
         <li> 
         <a href="http://localhost/d7t/m/sounds/tags" target="_self" class="button more_ntop_element">Tags</a> 
         </li> 
         <li> 
         <a href="http://localhost/d7t/m/sounds/categories" target="_self" class="button more_ntop_element">Categories</a> 
         </li> 
         <li> 
         <a href="http://localhost/d7t/m/sounds/rate" target="_self" class="button more_ntop_element">Rater</a> 
         </li> 
         <li> 
         <a href="http://localhost/d7t/m/sounds/calendar" target="_self" class="button more_ntop_element">Calendar</a> 
         </li> 
         <li> 
         <a href="http://localhost/d7t/searchKeyword.php?type=bx_sounds" target="_self" class="button more_ntop_element">Search</a> 
         </li> 
        </ul> 
       </div> 
       <a href="http://localhost/d7t/m/sounds/home/" class="top_link">Sounds</a> 
       <div class="clear_both sub"> 
       </div> 
       </li> 
       <li class=""> 
       <div class="sub"> 
        <ul class="sub main_elements"> 
         <li> 
         <a href="http://localhost/d7t/m/store/home/" target="_self" class="button more_ntop_element">Store Home</a> 
         </li> 
         <li> 
         <a href="http://localhost/d7t/m/store/browse/recent" target="_self" class="button more_ntop_element">Recent</a> 
         </li> 
         <li> 
         <a href="http://localhost/d7t/m/store/browse/top" target="_self" class="button more_ntop_element">Top Rated</a> 
         </li> 
         <li> 
         <a href="http://localhost/d7t/m/store/browse/popular" target="_self" class="button more_ntop_element">Popular</a> 
         </li> 
         <li> 
         <a href="http://localhost/d7t/m/store/browse/featured" target="_self" class="button more_ntop_element">Featured</a> 
         </li> 
         <li> 
         <a href="http://localhost/d7t/m/store/browse/free" target="_self" class="button more_ntop_element">Free</a> 
         </li> 
         <li> 
         <a href="http://localhost/d7t/m/store/tags" target="_self" class="button more_ntop_element">Tags</a> 
         </li> 
         <li> 
         <a href="http://localhost/d7t/m/store/categories" target="_self" class="button more_ntop_element">Categories</a> 
         </li> 
         <li> 
         <a href="http://localhost/d7t/m/store/calendar" target="_self" class="button more_ntop_element">Calendar</a> 
         </li> 
         <li> 
         <a href="http://localhost/d7t/m/store/search" target="_self" class="button more_ntop_element">Search</a> 
         </li> 
        </ul> 
       </div> 
       <a href="http://localhost/d7t/m/store/home/" class="top_link">Store</a> 
       <div class="clear_both sub"> 
       </div> 
       </li> 
       <li class="li_last_round">&nbsp;</li> 
      </ul> 
     </div> 
     <div class="clear_both"> 
     </div> 
     <!--[if lte IE 6]></td></tr></table></a><![endif]--> 
    </td> 
</tr> 
</tbody> 
</table> 

回答

1

这是怎样的一个很大的问题,而不仅仅是一个,和亲可以说并不是特别适合于stackoverflow。

你想要选择最后一个带顶级的td。这应该利用上一次的孩子选择做:

$("table.topMenu td.top:last-child") // you won't use this, just showing the hierarchy 

然后,从这个TD内,要选择每个子类的div的。

$("table.topMenu td.top:last-child div.sub") 

这应该选择你想要的div的所有内容。然后,你需要在这同一节选择像这样的链接的内容:

$("table.topMenu td.top:last-child a.top_link") 

你应该将它们存储到JavaScript对象,就像这样:

var divs = $("table.topMenu td.top:last-child div.sub").remove(); 
var links = $("table.topMenu td.top:last-child a.top_link").remove(); 

这些行会从DOM中删除,但保存jQuery对象供以后使用(请注意,由于这些选择器将获取多个DOM元素,因此它们是jQuery对象,而不是此时的DOM元素)。然后,您应该可以根据需要使用适当的选择器追加它们。您也可以拨打:

$("table.topMenu td.top:last-child").remove(); 

以及清理DOM。

+0

感谢您的指导,现在更容易:) –