2016-05-27 58 views
0

目標:如何將這些li元素組包裝在ul容器中?

首先包裹在一組ul容器的第一空div之前的元素,然後做是空li S之間的元件是相同的。

空元素可以被包含在容器中或被刪除。

問:

如何包裝這些元素集合在一組ul容器,如果我不能針對類?

問題:

1)I可以使用的唯一的變量是空li因爲會有不同的量在每個容器中的元素。

2)html不會以空的li結尾。

HTML:

<div class="body"> 
<fieldset> 
<ol> 

<li> 
    <h6>Customer Details</h6> 
</li> 
<li> 
    <h6>Customer Details</h6> 
</li> 

渦卷元件上方

<li>&nbsp;</li> 

下面渦卷元件,直到下一個空li

<li> 
    <h6>Customer Details</h6> 
</li> 
<li> 
    <h6>Customer Details</h6> 
</li> 
<li> 
    <h6>Customer Details</h6> 
</li> 
<li>&nbsp;</li> 

渦卷以下元素,直到下一個空li

<li> 
    <h6>Customer Details</h6> 
</li> 
<li> 
    <h6>Customer Details</h6> 
</li> 
<li> 
    <h6>Customer Details</h6> 
</li> 
<li> 
    <h6>Customer Details</h6> 
</li> 

</ol> 
</fieldset> 
</div> 
+0

'ul> div'不是有效的HTML。什麼是空div? '

','
\r\n
','
'...? – Justinas

+0

你爲什麼不試試爲你的問題預先加入jquery方法並附加它。因爲這需要像網站這樣的w3schools。 –

+0

我現在如何做基本的.wrap和.wrapAll函數,我的問題是,我現在不要如何定位列表幷包裝在ul容器中的空li元素之間的所有元素 - 我不能使用類或計數數字if訂單,因爲它改變,並沒有一個類。 –

回答

3

這是完全沒有經過測試的,因爲我在我的手機上,但如果有語法錯誤,我很樂意更新。它依賴於你能夠選擇/識別你提供的HTML的父代(假設你沒有添加上面評論中提到的最後一個空分隔符)。

// Get the known parent to this mess of li elements 
var knownParent = $(...); 
// Give the empty li elements an identifying class 
knownParent.find("li").filter(function(){ return $(this).html().match(/&nbsp;/) !== null; }).addClass("_liDivider"); 
// Wrap everything between the dividers with a ul element 
$.each(
    $("._liDivider:not(:last-child)"), function() { 
     $(this) 
     .nextUntil("._liDivider") 
     .wrap("<ul></ul>"); 
    } 
); 
// Now deal with the orphaned li elements before the first divider 
knownParent.find("li:first-of-type").nextUntil("._liDivider").wrap("<ul></ul>"); 
+0

我想這會用'ul'標籤包裝每個非空的'li'。沒有嘗試過這個,但是有類似的解決方案 –

+0

它不應該。從手冊:「獲取每個元素的所有後續兄弟姐妹,但不包括選擇器,DOM節點或jQuery對象匹配的元素通過」 –

+0

嗨,謝謝你的建議。由於某種原因,它不會給李元素類。它們被包裹在一個ol容器中,包裝在一個容器中 - 然後用一個.body類包裝 –