如何从相同的html元素对象的数组中获取html对象[数据值]

时间:2014-03-24 10:09:01

标签: jquery

如何从数组中包含多个like元素的html对象中获取数据对象。

<div id='show_box'>
    <h6 id="#0" data-choosed="1000">1000</h6>
    <h6 id="#1" data-choosed="1000">2000</h6>
    <h6 id="#2" data-choosed="1000">3000</h6>
</div> 

在javascript var h6_len=$("#show_box > h6").length;

         switch (h6_len) {
            case 0:
                 choosed=$('#show_box > #' + h6_len).data('choosed');

                $('#total_box').text(choosed);// this return the drop-down clicked
              //this part of the code is working fine.. when the case == 0
              break;
        case 1:
              var h6_1=$('#show_box > h6')[0];
                  var h6_2=$('#show_box > h6')[1];

                  /* this is where i am having issues... getting the data value from one of the array the H6 element...

                  console.log( typeof h6_1);
                  break;
            case 2:
                  console.log(h6_len);
                  break;
                  default:
                  $('#total_box').empty();
          }

1 个答案:

答案 0 :(得分:1)

<强> HTML

<div id='show_box'>
    <h6 id="#0" data-choosed="1000">1000</h6>
    <h6 id="#1" data-choosed="2000">2000</h6>
    <h6 id="#2" data-choosed="3000">3000</h6>
</div>

<强>的jQuery

$('h6').each(function () {
    console.log($(this).data('choosed'));
});

<强> Working fiddle 即可。希望这会有所帮助。