码迷,mamicode.com
首页 > 编程语言 > 详细

javascript遍历表

时间:2015-06-21 19:44:33      阅读:106      评论:0      收藏:0      [点我收藏+]

标签:

定义表结构

技术分享

1. 通过id遍历

<html>
<body>

<table id="tb" border="1">
<tr>
    <td>row1,cell1</td>    <td>row1,cell2</td>
</tr>

<tr>
    <td>row2,cell1</td>    <td>row2,cell2</td>
</tr>

<tr>
    <td>row3,cell1</td>    <td>row3,cell2</td>
</tr>
</table>
<button type="button" onclick="f()">click me</button>

<script>
function f()
{
    var t=document.getElementById("tb");
    if (t)
    {
        for(var i=0; i< t.rows.length; i++)
        {
            for(var j=0; j<t.rows[i].cells.length; j++)
            {
                alert(t.rows[i].cells[j].innerText);
            }
        }
    }
}
</script>

</body>
</html>

2. 通过jQuery遍历

<html>
<head>
<meta charset="utf-8">
</head>
<body>


<table class="tb" border="1">
<tr>
    <td>row1,cell1</td>    
    <td>row1,cell2</td>
</tr>

<tr>
    <td>row2,cell1</td>    
    <td>row2,cell2</td>
</tr>

</table>

<script type="text/javascript" src="./js/jquery-1.11.1.min.js"></script>

<script>
$(document).ready(function(){
        $("td").each(function(){
            alert($(this).text());
        })
})
</script>

</body>
</html>

添加按钮,按按钮时弹出

<html>
<head>
<meta charset="utf-8">
</head>
<body>


<table class="tb" border="1">
<tr>
    <td>row1,cell1</td>    
    <td>row1,cell2</td>
</tr>

<tr>
    <td>row2,cell1</td>    
    <td>row2,cell2</td>
</tr>

</table>
<button type="button">click me</button>

<script type="text/javascript" src="./js/jquery-1.11.1.min.js"></script>

<script>
$(document).ready(function(){
        $("button").click(function(){
            $("td").each(function(){
                alert($(this).text());
            })
        })
})
</script>

</body>
</html>

 

javascript遍历表

标签:

原文地址:http://www.cnblogs.com/kaituorensheng/p/4592226.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!