有没有办法使用 jQuery CSS 主题来设置 HTML 表格 (CSS) 的主题?
除了我的 HTML 表格看起来不同之外,我的所有组件看起来都属于一起。
有没有办法使用 jQuery CSS 主题来设置 HTML 表格 (CSS) 的主题?
除了我的 HTML 表格看起来不同之外,我的所有组件看起来都属于一起。
那里有一堆资源:
支持 ThemeRoller 的插件:
更新:这是我整理的表格样式:
<script type="text/javascript">
(function ($) {
$.fn.styleTable = function (options) {
var defaults = {
css: 'styleTable'
};
options = $.extend(defaults, options);
return this.each(function () {
input = $(this);
input.addClass(options.css);
input.find("tr").live('mouseover mouseout', function (event) {
if (event.type == 'mouseover') {
$(this).children("td").addClass("ui-state-hover");
} else {
$(this).children("td").removeClass("ui-state-hover");
}
});
input.find("th").addClass("ui-state-default");
input.find("td").addClass("ui-widget-content");
input.find("tr").each(function () {
$(this).children("td:not(:first)").addClass("first");
$(this).children("th:not(:first)").addClass("first");
});
});
};
})(jQuery);
$(document).ready(function () {
$("#Table1").styleTable();
});
</script>
<table id="Table1" class="full">
<tr>
<th>one</th>
<th>two</th>
</tr>
<tr>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
</tr>
</table>
CSS:
.styleTable { border-collapse: separate; }
.styleTable TD { font-weight: normal !important; padding: .4em; border-top-width: 0px !important; }
.styleTable TH { text-align: center; padding: .8em .4em; }
.styleTable TD.first, .styleTable TH.first { border-left-width: 0px !important; }
dochoffiday 的回答是一个很好的起点,但对我来说它并没有削减它(CSS 部分需要一个 buff),所以我做了一个修改后的版本,有几个改进。
(function ($) {
$.fn.styleTable = function (options) {
var defaults = {
css: 'ui-styled-table'
};
options = $.extend(defaults, options);
return this.each(function () {
$this = $(this);
$this.addClass(options.css);
$this.on('mouseover mouseout', 'tbody tr', function (event) {
$(this).children().toggleClass("ui-state-hover",
event.type == 'mouseover');
});
$this.find("th").addClass("ui-state-default");
$this.find("td").addClass("ui-widget-content");
$this.find("tr:last-child").addClass("last-child");
});
};
})(jQuery);
与原版的区别:
ui-styled-table
(听起来更一致).live
调用已替换.on
为 jQuery 1.7 以上推荐的调用.toggleClass
(更简洁的等效项)first
在表格单元格上设置误导性命名的 CSS 类的代码已被删除.last-child
到最后一个表格行的代码对于修复 Internet Explorer 7 和 Internet Explorer 8 上的视觉故障是必要的;对于支持:last-child
它的浏览器是没有必要的/* Internet Explorer 7: setting "separate" results in bad visuals; all other browsers work fine with either value. */
/* If set to "separate", then this rule is also needed to prevent double vertical borders on hover:
table.ui-styled-table tr * + th, table.ui-styled-table tr * + td { border-left-width: 0px !important; } */
table.ui-styled-table { border-collapse: collapse; }
/* Undo the "bolding" that jQuery UI theme may cause on hovered elements
/* Internet Explorer 7: does not support "inherit", so use a MS proprietary expression along with an Internet Explorer <= 7 targeting hack
to make the visuals consistent across all supported browsers */
table.ui-styled-table td.ui-state-hover {
font-weight: inherit;
*font-weight: expression(this.parentNode.currentStyle['fontWeight']);
}
/* Initally remove bottom border for all cells. */
table.ui-styled-table th, table.ui-styled-table td { border-bottom-width: 0px !important; }
/* Hovered-row cells should show bottom border (will be highlighted) */
table.ui-styled-table tbody tr:hover th,
table.ui-styled-table tbody tr:hover td
{ border-bottom-width: 1px !important; }
/* Remove top border if the above row is being hovered to prevent double horizontal borders. */
table.ui-styled-table tbody tr:hover + tr th,
table.ui-styled-table tbody tr:hover + tr td
{ border-top-width: 0px !important; }
/* Last-row cells should always show bottom border (not necessarily highlighted if not hovered). */
/* Internet Explorer 7, Internet Explorer 8: selector dependent on CSS classes because of no support for :last-child */
table.ui-styled-table tbody tr.last-child th,
table.ui-styled-table tbody tr.last-child td
{ border-bottom-width: 1px !important; }
/* Last-row cells should always show bottom border (not necessarily highlighted if not hovered). */
/* Internet Explorer 8 BUG: if these (unsupported) selectors are added to a rule, other selectors for that rule will stop working as well! */
/* Internet Explorer 9 and later, Firefox, Chrome: make sure the visuals are working even without the CSS classes crutch. */
table.ui-styled-table tbody tr:last-child th,
table.ui-styled-table tbody tr:last-child td
{ border-bottom-width: 1px !important; }
我已经在 Internet Explorer 7 及更高版本、Firefox 11 和 Google Chrome 18 上对此进行了测试,并确认它可以完美运行。我没有合理地测试过早期版本的 Firefox 和 Chrome 或任何版本的Opera;然而,这些浏览器以良好的 CSS 支持而闻名,由于我们在这里没有使用任何前沿功能,我认为它在那里也可以正常工作。
如果您对 Internet Explorer 7 的支持不感兴趣,可以使用一个 CSS 属性(由 star hack 引入)。
如果您对 Internet Explorer 8 支持也不感兴趣,那么与添加和定位CSS 类相关的 CSS和JavaScript 也可以。last-child
为什么不只使用表格中的主题样式?IE
<table>
<thead class="ui-widget-header">
<tr>
<th>Id</th>
<th>Description</th>
</td>
</thead>
<tbody class="ui-widget-content">
<tr>
<td>...</td>
<td>...</td>
</tr>
.
.
.
</tbody>
</table>
而且你不需要使用任何代码......
我有一个衬里可以使 HTML 表看起来像 BootStrapped:
<table class="table table-striped table-bordered table-hover">
该主题适合其他控件,它支持交替行突出显示。