使用jquery将HTML表转换为ul li

时间:2022-11-27 23:25:26

I want to convert HTML table to ul and li elements. my table structure is

我想将HTML表转换为ul和li元素。我的表结构

<table>
    <tbody>
        <tr>
            <th>1974</th>
            <td>UK</td>
        </tr>
        <tr>
                <th>1976</th>
            <td>Street</td>
        </tr>
        <tr>
            <th>1976-2002</th>
            <td>visitors.</td>
        </tr>

    </tbody> 
 </table>

I tried to convert this to

我试着把它转换成

        <ul>
            <li>
                <p>1974</p>
                <p>UK</p>
            </li>
            <li>
                    <p>1976</p>
                <p>Street</p>
            </li>
            <li>
                <p>1976-2002</p>
                <p>visitors.</p>
            </li>

        </ul> 

using jquery replaceWith function as below. Here I firstly going to convert <tbody> element.

使用jquery replaceWith函数如下所示。这里我首先要转换元素。

$(document).ready(function() {
    $("tbody").each(function(){
    var html = $(this).html();
    $(this).replaceWith("<ul>" + html + "</ul>");
    });
)};

but this not give any positive answer. Anybody have any idea how to do this.

但这并没有给出任何肯定的答案。大家都知道怎么做。

Thanks.

谢谢。

5 个解决方案

#1


8  

I would do it like this:

我会这样做:

$(document).ready(function() {
    var ul = $("<ul>");
    $("table tr").each(function(){
        var li = $("<li>")
        $("th, td", this).each(function(){
            var p = $("<p>").html(this.innerHTML);
            li.append(p);
        });
        ul.append(li);
    })    
    $("table").replaceWith(ul);    
});

UL first is created, and than table is replaced with UL.

首先创建UL,然后用UL替换than表。

Demo: http://jsfiddle.net/yXyCk/

演示:http://jsfiddle.net/yXyCk/

#2


6  

Try

试一试

$('table').replaceWith( $('table').html()
   .replace(/<tbody/gi, "<ul id='table'")
   .replace(/<tr/gi, "<li")
   .replace(/<\/tr>/gi, "</li>")
   .replace(/<td/gi, "<p")
   .replace(/<\/td>/gi, "</p>")
   .replace(/<th/gi, "<p")
   .replace(/<\/th>/gi, "</p>")
   .replace(/<\/tbody/gi, "<\/ul")
);

Above code is tested and worked for me..
Cheers

以上代码是为我测试和工作的。干杯

#3


0  

use this code

使用这个代码

function convertToList(element) {
    var list = $("<ul/>");

    $(element).find("tr").each(function() {
        var p = $(this).children().map(function() {
            return "<p>" + $(this).html() + "</p>";
        });

        list.append("<li>" + $.makeArray(p).join("") + "</li>");
    });

    $(element).replaceWith(list);
}

#4


0  

The problem with the other answers here is that all of the styles are lost when converting the table to a list. This is what I used to accomplish it and maintain the table look:

这里的其他答案的问题是,当将表转换为列表时,所有样式都会丢失。这是我用来完成它并保持桌面外观的东西:

function convert_table_to_list($table) {
    var $list = $("<ul></ul>");
    $list.css("list-style-type", "none");
    $list.css("padding", 0);

    $table.find("tr").each(function() {
        var $li = $("<li></li>");

        $li.height($(this).height());
        $(this).find("td").each(function() {
            var $div = $("<div></div>");

            // Add inline and external styling
            var style = css($(this));
            $div.css(style);

            // Add some browser default styles
            $div.css("display", "table-cell");
            $div.html($(this).html());
            $div.width($(this).width());
            $div.height($(this).height());
            $div.css("vertical-align", $(this).css("vertical-align"));
            $div.css("padding", $(this).css("padding"));

            // Add class(es)
            $div.addClass($(this).attr("class"));

            // Append to li
            $li.append($div);
        });
        // Append li to ul
        $list.append($li);
    });

    $list.insertAfter($table);
    $table.remove();

    return $list;
}

// The following are taken from http://*.com/questions/754607/can-jquery-get-all-css-styles-associated-with-an-element
function css(a) {
    var sheets = document.styleSheets, o = {};
    for (var i in sheets) {
        var rules = sheets[i].rules || sheets[i].cssRules;
        for (var r in rules) {
            if (a.is(rules[r].selectorText)) {
                o = $.extend(o, css2json(rules[r].style), css2json(a.attr('style')));
            }
        }
    }
    return o;
}
function css2json(css) {
    var s = {};
    if (!css) return s;
    if (css instanceof CSSStyleDeclaration) {
        for (var i in css) {
            if ((css[i]).toLowerCase) {
                s[(css[i]).toLowerCase()] = (css[css[i]]);
            }
        }
    } else if (typeof css == "string") {
        css = css.split("; ");
        for (var i in css) {
            var l = css[i].split(": ");
            s[l[0].toLowerCase()] = (l[1]);
        }
    }
    return s;
}

See it on JSFiddle

看到它在JSFiddle

#5


0  

Convert <table> structure menu to <UL> using jQuery

使用jQuery将

结构菜单转换为

I have used for loop instead if each for better speed/performance

我用for循环来代替if循环,以获得更好的速度和性能

   for(var i=0; i<$anchorTarget.length; i++){
            var $eachAnchor = $anchorTarget.eq(i);
            //get anchor markup from table
            var getAnchor = $eachAnchor.parent().html();
            //append anchor on created ul
            $("."+convertedMenuCls).append("<li>"+getAnchor+"</li>");
            //hide old icon - if you've icon as a image in old table markup
            $eachAnchor.parent().parent().find("img").hide();
            //Add new icon
            $eachAnchor.parent().parent().find("img").after("<span>+</span>");
        }

Table structure

表结构

使用jquery将HTML表转换为ul li

UL Structure After conversion 使用jquery将HTML表转换为ul li

UL结构转换后

Here is working code: Convert Table structure menu to UL using jQuery

这里是工作代码:使用jQuery将表结构菜单转换为UL。

#1


8  

I would do it like this:

我会这样做:

$(document).ready(function() {
    var ul = $("<ul>");
    $("table tr").each(function(){
        var li = $("<li>")
        $("th, td", this).each(function(){
            var p = $("<p>").html(this.innerHTML);
            li.append(p);
        });
        ul.append(li);
    })    
    $("table").replaceWith(ul);    
});

UL first is created, and than table is replaced with UL.

首先创建UL,然后用UL替换than表。

Demo: http://jsfiddle.net/yXyCk/

演示:http://jsfiddle.net/yXyCk/

#2


6  

Try

试一试

$('table').replaceWith( $('table').html()
   .replace(/<tbody/gi, "<ul id='table'")
   .replace(/<tr/gi, "<li")
   .replace(/<\/tr>/gi, "</li>")
   .replace(/<td/gi, "<p")
   .replace(/<\/td>/gi, "</p>")
   .replace(/<th/gi, "<p")
   .replace(/<\/th>/gi, "</p>")
   .replace(/<\/tbody/gi, "<\/ul")
);

Above code is tested and worked for me..
Cheers

以上代码是为我测试和工作的。干杯

#3


0  

use this code

使用这个代码

function convertToList(element) {
    var list = $("<ul/>");

    $(element).find("tr").each(function() {
        var p = $(this).children().map(function() {
            return "<p>" + $(this).html() + "</p>";
        });

        list.append("<li>" + $.makeArray(p).join("") + "</li>");
    });

    $(element).replaceWith(list);
}

#4


0  

The problem with the other answers here is that all of the styles are lost when converting the table to a list. This is what I used to accomplish it and maintain the table look:

这里的其他答案的问题是,当将表转换为列表时,所有样式都会丢失。这是我用来完成它并保持桌面外观的东西:

function convert_table_to_list($table) {
    var $list = $("<ul></ul>");
    $list.css("list-style-type", "none");
    $list.css("padding", 0);

    $table.find("tr").each(function() {
        var $li = $("<li></li>");

        $li.height($(this).height());
        $(this).find("td").each(function() {
            var $div = $("<div></div>");

            // Add inline and external styling
            var style = css($(this));
            $div.css(style);

            // Add some browser default styles
            $div.css("display", "table-cell");
            $div.html($(this).html());
            $div.width($(this).width());
            $div.height($(this).height());
            $div.css("vertical-align", $(this).css("vertical-align"));
            $div.css("padding", $(this).css("padding"));

            // Add class(es)
            $div.addClass($(this).attr("class"));

            // Append to li
            $li.append($div);
        });
        // Append li to ul
        $list.append($li);
    });

    $list.insertAfter($table);
    $table.remove();

    return $list;
}

// The following are taken from http://*.com/questions/754607/can-jquery-get-all-css-styles-associated-with-an-element
function css(a) {
    var sheets = document.styleSheets, o = {};
    for (var i in sheets) {
        var rules = sheets[i].rules || sheets[i].cssRules;
        for (var r in rules) {
            if (a.is(rules[r].selectorText)) {
                o = $.extend(o, css2json(rules[r].style), css2json(a.attr('style')));
            }
        }
    }
    return o;
}
function css2json(css) {
    var s = {};
    if (!css) return s;
    if (css instanceof CSSStyleDeclaration) {
        for (var i in css) {
            if ((css[i]).toLowerCase) {
                s[(css[i]).toLowerCase()] = (css[css[i]]);
            }
        }
    } else if (typeof css == "string") {
        css = css.split("; ");
        for (var i in css) {
            var l = css[i].split(": ");
            s[l[0].toLowerCase()] = (l[1]);
        }
    }
    return s;
}

See it on JSFiddle

看到它在JSFiddle

#5


0  

Convert <table> structure menu to <UL> using jQuery

使用jQuery将

结构菜单转换为

I have used for loop instead if each for better speed/performance

我用for循环来代替if循环,以获得更好的速度和性能

   for(var i=0; i<$anchorTarget.length; i++){
            var $eachAnchor = $anchorTarget.eq(i);
            //get anchor markup from table
            var getAnchor = $eachAnchor.parent().html();
            //append anchor on created ul
            $("."+convertedMenuCls).append("<li>"+getAnchor+"</li>");
            //hide old icon - if you've icon as a image in old table markup
            $eachAnchor.parent().parent().find("img").hide();
            //Add new icon
            $eachAnchor.parent().parent().find("img").after("<span>+</span>");
        }

Table structure

表结构

使用jquery将HTML表转换为ul li

UL Structure After conversion 使用jquery将HTML表转换为ul li

UL结构转换后

Here is working code: Convert Table structure menu to UL using jQuery

这里是工作代码:使用jQuery将表结构菜单转换为UL。