日期:2014-05-16  浏览次数:20694 次

jquery的赋值问题 - Web 开发 / Ajax
xml文件片段:

。。。。
<table>
<tbody>
  <tr>
  <td> 1 </td>
  <th> 300 </th>
  </tr>

  <tr>
  <td> 2 </td>
  <th> 400 </th>
  </tr>
</tbody>
</table>
。。。。


我现在想读取其中<td>和<th>中的数据,分别存入x,y中去

  $('tr',tbody).each(function(){
  data.push({
  x:td中的数据
  y:th中的数据
  });

  });



其中 x:td中的数据
  y:th中的数据

应该怎么写?谢谢

------解决方案--------------------
HTML code

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript">
var data = [];
$(function(){
    $('tr').each(function(){
        var x = $(this).find("td:first").html();
        var y = $(this).find("th").html();
        data.push({
            x:x,
            y:y
        });
        alert(x + " " + y)
    })
    alert(data.length)
})
</script>
<table>
<tbody>
  <tr>
  <td>1</td>
  <th>300</th>
  </tr>
  <tr>
  <td>2</td>
  <th>400</th>
  </tr>
</tbody>
</table>