2015-04-12 93 views
0

我需要使用帶有JQuery UI Accordion的表格。 標題和內容都應該是具有固定寬度的3列的表格。 由於一些奇怪的原因,內容的第一列使用忽略css的所有空間,儘管標題看起來不錯。 這裏是我的jsfiddle: http://jsfiddle.net/qm12h9tL/JQuery手風琴寬度錯誤

這裏是代碼:

<script> 
$(document).ready(function() { 
    $('#topics').accordion({ 
     collapsible:true, 
     header:'.topic', 
     content:'.lessons' 
    }); 
}); 
</script> 
<style> 
body { 
    width: 400px; 
    max-width: 400px; 
    min-width: 400px; 
} 

.accordion-container { 
    width:100%; 
} 

.topic, .lessons { 
    width: 400px; 
} 

.name { 
    width: 70%; 
} 

.cards { 
    width:15%; 
} 

.percentage { 
    width: 15%; 
} 
</style> 
<div class="accordion-container"> 
    <table id="topics"> 
     <tbody class="topic" style="display:table;" id="t1"> 
      <tr> 
       <td class="name">Topic 1</td> 
       <td class="cards">10 cards</td> 
       <td class="percentage">100%</td> 
      </tr> 
     </tbody> 
     <tbody class="lessons"> 
      <tr> 
       <td class="name">Lesson 1</td> 
       <td class="cards">10 cards</td> 
       <td class="percentage">100%</td> 
      </tr> 
     </tbody> 
    </table> 
</div> 

EDIT1我試着編寫HTML代碼是這樣的:

<table id="topics"> 
     <tr class="topic"> 
      <td class="name">Topic 1</td> 
      <td class="cards">10 cards</td> 
      <td class="percentage">100%</td> 
     </tr> 
     <tr class='lessons'> 
      <td class="name">Lesson 1</td> 
      <td class="cards">10 cards</td> 
      <td class="percentage">100%</td> 
     </tr> 
</table> 

也不起作用。

編輯2我從來沒有想過,增加更多的課程到一個主題將是一個問題! http://jsfiddle.net/qm12h9tL/11/

<table id="topics"> 
    <tbody id="t1"> 
     <tr style="display: table-row;" class="topic"> 
      <td class="name">Topic 1</td> 
      <td class="cards">10 cards</td> 
      <td class="percentage">100%</td> 
     </tr> 
     <tr class="lesson"> 
      <td class="name">Lesson 1</td> 
      <td class="cards">10 cards</td> 
      <td class="percentage">100%</td> 
     </tr> 
     <tr class="lesson"> 
      <td class="name">Lesson 1</td> 
      <td class="cards">10 cards</td> 
      <td class="percentage">100%</td> 
     </tr> 
    </tbody> 
</table> 

+0

爲什麼你恢復到以前的版本不正確的代碼格式?我明白如果你不同意我對這個問題所做的修改,但至少你應該讓人們在不訪問外部網站的情況下運行代碼。 –

+0

我看到的是劃掉的單詞和寫在他們的地方的單詞 - 爲了編輯而在我看來喜歡編輯。我會審查它。 –

回答

0

顯示器:表樣式屬性被搞砸了佈局(你的第一個TBODY標籤)。也許這是一些複製/粘貼操作的剩餘部分?

+1

即使在刪除'display:table'後,列似乎被扭曲 – Dhiraj

+0

我已經添加它以嘗試修復列。刪除它不起作用。 –

+0

它在那裏把表格佈局搞砸了,所以它讓你的問題變得更糟。您可以通過查看它而不使用jquery init代碼來檢查它。 jQuery在tbody元素內插入一個不允許在那裏的跨度。因此,您使用手風琴的方式不適合使用。 – Markus

1

實際上,您爲您的標題(<tbody>)元素添加了display: table屬性。這擰了佈局的一部分。當你刪除它時,jQuery UI會扭轉你的佈局。它將display: block添加到變成塊的<tr>元素。我建議您將display: table-row設置爲您的<tr>元素。

您現在正面臨另一個問題,因爲jQuery會在您的<tr>中添加一個帶有圖標的跨度,該圖標呈現爲額外的單元格。添加此CSS:

table span 
{ 
    display: none !important; 
} 

而且會解決它。

更新小提琴:http://jsfiddle.net/qm12h9tL/8/


更新:固定collapsible: true

+0

它確實修復了佈局,但手風琴似乎沒有被設計用於tbody聲明。至少在Firefox和Chrome中,它並不像您期望手風琴那樣行事。編輯:至少它現在選擇文本到處都是,即使它崩潰和擴大。但更一般地說,我認爲不應該將顯示器添加到不存在的項目中。 – Markus

+0

這是對的,但它是一個簡單的臨時解決方法。而且即使在崩潰之前有一小段延遲,它在FF和Chrome中似乎也能正常工作。 –

+0

現在爲什麼角落不再圓? –

0

一個黑客已經取得的解決方案

.ui-accordion .ui-accordion-header{ 
    display: table-caption !important; 
} 

請注意display: table-caption;不會在IE7和更早版本支持。 IE8需要!DOCTYPE。 IE9支持這些值。

$(document).ready(function() { \t 
 
\t $('#topics').accordion({ 
 
\t \t collapsible:true, 
 
\t \t header:'.topic', 
 
\t \t content:'.lessons' 
 
\t }); 
 
});
.accordion-container { 
 
    width: 100%; 
 
    background-color: #ff0; 
 
} 
 
#topics{ 
 
    width: 400px; 
 
} 
 
.name { 
 
    width: 70%; 
 
} 
 
.cards { 
 
    width: 15%; 
 
} 
 
.percentage { 
 
    width: 15%; 
 
} 
 
.ui-accordion .ui-accordion-header{ 
 
    display: table-caption !important; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.11.3/jquery-ui.min.js"></script> 
 
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.11.3/themes/south-street/jquery-ui.css" rel="stylesheet" /> 
 
<div class="accordion-container"> 
 
    <table id="topics"> 
 
    <tbody class="topic" id="t1"> 
 
     <tr> 
 
     <th class="name">Topic 1</th> 
 
     <th class="cards">10 cards</th> 
 
     <th class="percentage">100%</th> 
 
     </tr> 
 
    </tbody> 
 
    <tbody class="lessons"> 
 
     <tr> 
 
     <td class="name">Lesson 1</td> 
 
     <td class="cards">10 cards</td> 
 
     <td class="percentage">100%</td> 
 
     </tr> 
 
    </tbody> 
 
    </table> 
 
</div>

+0

不適用於寬度= 700px; 我設置了width = 400px,因爲它在JSFiddle中看起來更好。 –