English 中文(简体)
如何用 jquery 转换 html 表格列表?
原标题:How to Convert html table to list using jquery?
  • 时间:2012-05-23 07:45:38
  •  标签:
  • jquery
  • html

I want to convert the below table to unordered list: (This post not giving the exact result: How to transform HTML table to list with JQuery?)

我这样尝试过: < / manger>

     <script type="text/javascript">

        $(document).ready(function(){

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

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

            $(table).replaceWith(list);

        });
    </script> 


<div class="productTable">
    <table cellspacing="2" cellpadding="2" border="0">
    <tr>
    <td colspan="2"><br>
    <a href="">back to shop by all brands</a>
    </td>
    </tr>
    <tr>
    <td colspan="2"><a href="">back to "P" brands</a></td>
    </tr>
    <tr><td colspan="2"><h1>Paris</h1></td></tr><tr><td valign="top">
    <table cellpadding="1" cellspacing="2" border="0">
    <tr><td valign="top"><img src="" border="0"></td>
    <td></td></tr>
    </table>
    </td>
    </tr>
    <tr><td style="background: #80826C;color: white;text-align:center;font-weight:bold" width="100%">
    Click a department below to see more products from this brand</td></tr>
    </table>
    <table cellspacing="2" cellpadding="2" border="0" width="100%" bgcolor="#e7e7e2">
    <tr><td valign="top"><a href="">Sleds, Skates & Toboggans</a></td>
    <td><img src="img/shim.gif" border="0" width="10" height="1"></td></table>
    <table border="0" cellpadding="0" cellspacing="0">
    <tr><td><img src="img/shim.gif" width="1" height="10" border="0"></td></tr>
    <tr><td style="background: #80826C;color: white;text-align:center;font-weight:bold" width="100%">A few of our products from this brand</td></tr>
    <tr><td><img src="img/shim.gif" width="1" height="10" border="0"></td></tr>
    <tr><td><table><tr><td valign="top" align="center" width="155"><a href="">
    <img src="" border="0" alt="Paris Standard Child Back Pad for E-32 Sled" title="Paris Standard Child Back Pad for E-32 Sled"></a><br>
    <a href="">Paris Standard Child Back Pad for E-32 Sled</a><br>
    <img src="img/shim.gif" width="1" height="19" border="0"><strong>Price:</strong> <strong>$29.99</strong></td><td></td><td></td></tr></table></td></tr></table>
    <table cellspacing="2" cellpadding="2" border="0" width="100%"><tr><td colspan="2"><br><br><a href="">back to "P" brands</a></td></tr>
    <tr><td colspan="2"><br><a href="">back to shop by all brands</a>
    </td></tr>
    </table>
</div>
最佳回答
    $(document).ready(function () {
                $( table ).each(function () {
                    var list = $("<ul/>");

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

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

                    $(this).replaceWith(list);
                });
            });


//Or 



        $(document).ready(function () {
            $( div.productTable table ).each(function () {
                var list = $("<ul/>");

                $(this).find("tr").each(function () {
                    var p = $(this).children().map(function () {
                        if ($(this).find( table ).length > 0) {
                            var subList = $("<ul/>");

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

                            subList.append("<li>" + $.makeArray(sP).join("") + "</li>");

                            return $( <div /> ).append($( <p /> ).append(subList)).html();
                        }
                        else {
                            return "<p>" + $(this).html() + "</p>";
                        }
                    });

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

                $(this).replaceWith(list);
            });
        });
问题回答

暂无回答




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签