Jquery 解析 XML - 交替行颜色

发布于 2024-10-31 09:13:11 字数 2543 浏览 0 评论 0原文

如何向以下内容添加代码以交替行颜色

parsedata = function(xml){        
            var str = "";
            var cart = xml.documentElement.firstChild;
            if(cart.childNodes.length > 0 ){                
                str = str + '<table border="0" width="100%" cellspacing="0" cellpadding="5">';
                str = str + '  <tr><td class="animBoxCartLink" colspan="2"><a href="' + globals.cart_link + '">' + globals.cart_text + '</a></td></tr>
                for (var i = 0; i < cart.childNodes.length; i++){               
                    try{name =  cart.getElementsByTagName("NAME")[i].childNodes[0].nodeValue;}catch(e){name = "Item";}
                    try{attributes =  cart.getElementsByTagName("ATTRIBUTES")[i].childNodes[0].nodeValue;}catch(e){attributes = "";}
                    try{llink =  cart.getElementsByTagName("LINK")[i].childNodes[0].nodeValue;}catch(e){llink = "";}
                    try{image =  cart.getElementsByTagName("IMAGE")[i].childNodes[0].nodeValue;}catch(e){image = "No Image";}
                    try{qty =  cart.getElementsByTagName("QTY")[i].childNodes[0].nodeValue;}catch(e){qty = "message";}
                    try{price =  cart.getElementsByTagName("PRICE")[i].childNodes[0].nodeValue;}catch(e){price = "$0.00";}

                    dimension = (globals.cart_image_width ? 'width="' + globals.cart_image_width : '') + (globals.cart_image_height ? '" height="' + globals.cart_image_height + '"' : '');

                str = str + '  <tr>';
                str = str + '    <td class="animBoxCartImage" width="' + globals.cart_image_width + '" align="center"><a href="' + llink +'"><img src="' + image + '" ' + dimension + ' border="0" alt="' + name + '"></a></td>';
                str = str + '    <td class="animBoxCartContent" width="100%">';
                str = str + '      <div class="animBoxCartName"><a href="' + llink + '">' + name + '</a><br />' + attributes + '</div>';
                str = str + '      ' + globals.text_cart_quantity + ' ' + qty;
                str = str + '      <div class="animBoxCartPrice">' + price + '</a></div>';
                str = str + '      <a href="' + llink + '"> More Info </a>';
                str = str + '    </td>';
                str = str + '  </tr>';

.js 文件(Jquery)的前半部分主要涉及位于标题中的购物车的显示/隐藏,我刚刚发布的部分是我的内容我的想法是我需要添加代码,我可以在其中添加样式以便行颜色交替。

How do I add code to the following to alternate the row color

parsedata = function(xml){        
            var str = "";
            var cart = xml.documentElement.firstChild;
            if(cart.childNodes.length > 0 ){                
                str = str + '<table border="0" width="100%" cellspacing="0" cellpadding="5">';
                str = str + '  <tr><td class="animBoxCartLink" colspan="2"><a href="' + globals.cart_link + '">' + globals.cart_text + '</a></td></tr>
                for (var i = 0; i < cart.childNodes.length; i++){               
                    try{name =  cart.getElementsByTagName("NAME")[i].childNodes[0].nodeValue;}catch(e){name = "Item";}
                    try{attributes =  cart.getElementsByTagName("ATTRIBUTES")[i].childNodes[0].nodeValue;}catch(e){attributes = "";}
                    try{llink =  cart.getElementsByTagName("LINK")[i].childNodes[0].nodeValue;}catch(e){llink = "";}
                    try{image =  cart.getElementsByTagName("IMAGE")[i].childNodes[0].nodeValue;}catch(e){image = "No Image";}
                    try{qty =  cart.getElementsByTagName("QTY")[i].childNodes[0].nodeValue;}catch(e){qty = "message";}
                    try{price =  cart.getElementsByTagName("PRICE")[i].childNodes[0].nodeValue;}catch(e){price = "$0.00";}

                    dimension = (globals.cart_image_width ? 'width="' + globals.cart_image_width : '') + (globals.cart_image_height ? '" height="' + globals.cart_image_height + '"' : '');

                str = str + '  <tr>';
                str = str + '    <td class="animBoxCartImage" width="' + globals.cart_image_width + '" align="center"><a href="' + llink +'"><img src="' + image + '" ' + dimension + ' border="0" alt="' + name + '"></a></td>';
                str = str + '    <td class="animBoxCartContent" width="100%">';
                str = str + '      <div class="animBoxCartName"><a href="' + llink + '">' + name + '</a><br />' + attributes + '</div>';
                str = str + '      ' + globals.text_cart_quantity + ' ' + qty;
                str = str + '      <div class="animBoxCartPrice">' + price + '</a></div>';
                str = str + '      <a href="' + llink + '"> More Info </a>';
                str = str + '    </td>';
                str = str + '  </tr>';

The first half of the .js file (Jquery) pertains mostly to the show/hide of the cart which is located in the header and the portion I just posted is what I'm thinking is where I would need to add code where I can add styles so that the row color alternates.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

愁杀 2024-11-07 09:13:11

保持其余代码相同:

str = str + ' '; 替换为:

str = str + '  <tr class="rowcolor' + (i%2) + '">';

然后您需要为 rowcolors 定义 css 类:

.rowcolor0 {
    background-color: #ffffff;
}

.rowcolor1 {
    background-color: #dddddd;
}

Keeping the rest of your code the same:

Replace str = str + ' <tr>'; with:

str = str + '  <tr class="rowcolor' + (i%2) + '">';

Then you need css classes defined for the rowcolors:

.rowcolor0 {
    background-color: #ffffff;
}

.rowcolor1 {
    background-color: #dddddd;
}
╰◇生如夏花灿烂 2024-11-07 09:13:11

我什至不打算尝试解决这个问题。在 tr 上:

str += '  <tr>';

添加 alt row 类:

str += '  <tr class="' + i%2 == 0 ? 'altrow' : '' + '">';

I'm not even going to try to fix this. On the tr:

str += '  <tr>';

add alt row class:

str += '  <tr class="' + i%2 == 0 ? 'altrow' : '' + '">';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文