如何在for循环中显示特定的子节点

时间:2015-04-03 18:57:06

标签: jquery html child-nodes

在我目前的html代码中,我有一个按以下顺序包含的表:

股票ID |产品|正常价格|销售价格

我面临的问题是,当我点击行中的值并将它们附加到另一个表或购物车时,我的for循环会附加整行。 我要找的只是附加产品,销售价格,然后我计算的"保存的金额"价钱。如何附加索引eq(1)[Product]和eq(3)[Sale Price]?根据我的理解,我应该使用另一种.find方法但不确定如何完全这样做。 (仍然让整个.find和子节点失效)

代码段:https://jsfiddle.net/ko828bcs/

Ps:我有jquery而不是javascript因此它不会在jsfiddle中运行(我只是希望你能够看到格式化)

我完成所有工作的代码是:

//add the selected item in table
var variable = "<tr class='s-list' id='s-"+id +"'>";
for (var i=0; i<child_nodes.length; i++){
        // add a table cell for each node using its content
        variable += "<td>" +child_nodes.eq(i).text() + "</td>";
} // end for
    variable += "</tr>";
    // add msg to the selected courses list
    $('#selected-list').append(variable);

然而,你可以看到我使用eq(i)给出整行。

文件:

<script>

//makes gray in table
$(document).ready(function() {
$(".list:odd").css('background-color', '#eee');
});

$(function() {

    $('#selected-list').on('click', 's-list',function(){

        alert($(this).children().eq(0).text());
        //delete this element
        $(this).remove();
    });

    $('.list').on('click', function(){

        //rest background color
        $('.list').css('background-color', '#fff');

        //change background color
        $(this).css('background-color', '#eee');        

        //read its id attribute
        var id = $(this).attr('id');
        //alert(id);

        //Check if it has already been added
        if ($('#selected-list').find('#s-'+id).length > 0){

            alert("The product is already selected. Please choose a different one!");
        }
        else {

        // find  child nodes
        var child_nodes = $(this).children();
        // read current total
        var current_total = parseInt($('#total').val());
        //read selected sale price
        var selected_sale= parseInt(child_nodes.eq(3).text());
        //read initial price
        var selected_price = parseInt(child_nodes.eq(2).text());            
        //saving amount for each selected
        var savings = selected_price - selected_sale;


        //add the selected item in table and update total
        var variable = "<tr class='s-list' id='s-"+id +"'>";
        for (var i=0; i<child_nodes.length; i++){
            // add a table cell for each node using its content
            variable += "<td>" +child_nodes.eq(i).text() + "</td>";
        } // end for
        variable += "</tr>";
        // add msg to the selected courses list
        $('#selected-list').append(variable);


        //update total cost
        // read the current total
        var total = parseInt($('#total').val());
        // add the selected item cost to the total
        total += parseInt(child_nodes.eq(3).text());

        // update the total cost
        $('#total').val(total); 

        // Reset background color of all the rows
        $('.courselist').css('background-color', '#fff');

        // Change background color of the selected row
        $(this).css('background-color', '#eee');
        }


    });
});


</script>

</head>
<body>
<h2>Sam's Store</h2>
<div class="content">
<div class='title'>Deals of the  Week</div>
<div class='labels'>
<div class='cell1' id='sid1'>Stock ID</div>
<div class='cell2' id='pid1'>Product</div>
<div class='cell3' id='rpid1'>Regular Price</div>
<div class='cell4' id='spid1'>Sale Price</div>
</div>

<div class='list' id='c1'>
<div class='cell1' id='sid2'>SH32AQ60</div>
<div class='cell2' id='pid2'>Sharp AQUAS-60in  HDTV</div>
<div class='cell3' id='rpid2'>799.99</div>
<div class='cell4' id='spid2'>759.99</div>
</div>
<div class='list' id='c28'>
<div class='cell1' id='sid3'>PN455</div>
<div class='cell2' id='pid13'>Panasonic 55in LED HDTV</div>
<div class='cell3' id='rpid3'>999.99</div>
<div class='cell4' id='spid3'>679.99</div>
</div>
<div class='list' id='c60'>
<div class='cell1' id='sid4'>VZ49M</div>
<div class='cell2' id='pid4'>VIZIO M series 49in LED HDTV</div>
<div class='cell3' id='rpid4'>719.99</div>
<div class='cell4' id='spid4'>579.99</div>
</div>
<div class='list' id='c62'>
<div class='cell1' id='sid5'>IN200SR</div>
<div class='cell2' id='pid5'>Insignia 200W Stereo Receiver</div>
<div class='cell3' id='rpid5'>24.99</div>
<div class='cell4' id='spid5'>19.99</div>
</div>

<div class='list' id='c6'>
<div class='cell1' id='sid6'>PN1000HTS</div>
<div class='cell2' id='pid6'>Panasonic 1000W Smart Blu-ray Home Theater System</div>
<div class='cell3' id='rpid6'>349.99</div>
<div class='cell4' id='spid6'>299.99</div>
</div>
<div class='list' id='c7'>
<div class='cell1' id='sid7'>HP305DJ</div>
<div class='cell2' id='pid7'>HP Deskjet 3056A</div>
<div class='cell3' id='rpid7'>49.90</div>
<div class='cell4' id='spid7'>34.90</div>
</div>
<div class='list' id='c8'>
  <div class='cell1' id='sid8'>RF386BMR</div>
  <div class='cell2' id='pid8'>Rocketfish Bluetooth Music Receiver</div>
  <div class='cell3' id='rpid8'>49.90</div>
  <div class='cell4' id='spid8'>34.90</div>
</div>
<div class='list' id='c9'>
    <div class='cell1' id='sid9'>SM350SB</div>
    <div class='cell2' id='pid9'>Sling Media Slingbox 350</div>
    <div class='cell3' id='rpid9'>179.90</div>
    <div class='cell4' id='spid9'>115.90</div>
</div>


</div>

<div id="shop">
 <div class="shoppinglist">
<div class='list-title'>You have <span id="items">0</span>  items in your Shopping Cart</div>
  <div >

  <div class='cell2'>Product</div>
  <div class='cell3'>Sale Price</div>
  <div class='cell4'>You Save</div>

  <div class='table selected-list' id='selected-list'></div>
  </div>
  </div>
 </div>
<div class="total">Total Amount: $<span id="total"></span></div>

</body>
</html>

1 个答案:

答案 0 :(得分:1)

你可以在循环中使用if条件

 for (var i=0; i<child_nodes.length; i++){
        if(i == 1 || i == 3)
        {
            variable += "<td>" +child_nodes.eq(i).text() + "</td>";
        }
    } // end for
    variable += "</tr>";

或者只是添加你想要的两个节点

    var variable = "<tr class='s-list' id='s-"+id +"'>";
    variable += "<td>" +child_nodes.eq(1).text() + "</td>";
    variable += "<td>" +child_nodes.eq(3).text() + "</td>";
    variable += "</tr>";