2015-04-12 92 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中看起来更好。 –