开发者

Jquery Parsing XML - Alternating Row Colour

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(开发者_如何学JAVAe){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.


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;
}


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' : '' + '">';
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜