jquery - 更有效的建表方法?

标签 jquery jquery-selectors

我必须将表格的 html 发送到 html 到 pdf 转换器脚本。我的表必须由 <thead> 组成从表 1 中,<tfoot>从表3和<tbody>从表2.我正在将 html 与 jQuery 放在一起。这就是我所拥有的:

$(document).ready(function() {
    var html = $('div.dataTables_scroll').html();//so that changes don't affect the main page
    var h = $(html).find('thead')[0];
    var f = $(html).find('tfoot')[1];
    var b = $(html).find('tbody')[0];
    var newtable = $('<table></table>').append(h, f, b);
    var d = $('<div></div>').append(newtable);
    $('#foo').val(d.html()); //to see what the html looks like   
});

Here是整个事情的 JSFiddle。它工作得很好,但我认为应该有一种更优雅的方式。

想法?

最佳答案

这个怎么样:

var ctx = $( 'div.dataTables_scroll' )[0];

var html = [
    '<table>',
        '<thead>' + $( 'thead', ctx ).eq( 0 ).html() + '</thead>',
        '<tfoot>' + $( 'tfoot', ctx ).eq( 1 ).html() + '</tfoot>',
        '<tbody>' + $( 'tbody', ctx ).eq( 0 ).html() + '</tbody>',
    '</table>'
].join( '' );

关于jquery - 更有效的建表方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8825526/

相关文章:

javascript - 如何在 AJAX 调用后继续提交表单?

javascript - 为什么我的跨度被剥离了

jquery - CSS 伪类选择器 :first - What is it? 我在示例代码中找到,但找不到有关它的文档

javascript - 使用 JavaScript 隐藏特定的 HTML 表格单元格

jquery - 如何在jquery中选择div内的两个元素

javascript - 检测浏览器对 css 属性的支持

javascript - jQuery UI 拖放鼠标位置问题

javascript - 类似 iPhone 的 jQuery 滚动条

jquery - 在 jquery 选择器中使用 >

jquery 动态 css 选择器不区分大小写