[英]jQuery extract content, switch content inside, wrap it in new tags
我整天都在嘗試重新排序此菜單,但我是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 li內部的內容,並切換與div的鏈接,然后將其包裝在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/&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/&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/&action=popular" target="_self" class="button more_ntop_element">Popular</a>
</li>
<li>
<a href="http://localhost/d7t/m/poll/&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"> </li>
</ul>
</div>
<div class="clear_both">
</div>
<!--[if lte IE 6]></td></tr></table></a><![endif]-->
</td>
</tr>
</tbody>
</table>
這是很多問題,而不僅僅是一個問題,並且可能不是特別適合Stackoverflow。
您要選擇最后一個帶有類top的td。 這應該使用最后一個子選擇器完成:
$( "table.topMenu td.top:last-child" ) // you won't use this, just showing the hierarchy
然后,在此td中,您要選擇每個帶有class sub的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。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.