`
qinya06
  • 浏览: 583506 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

js 表格

阅读更多
data = [{"username":"张三","age":"25"},{"username":"李四","age":"30"}]
function show(){   
    var data = ajax.responseText;//返回数据---JSON格式字符串   
    data = data.replace(/^\s*/, "").replace(/\s*$/, "");//取出返回数据里面多余的空值   
    data = eval('('+data+')'); //将JSON格式字符串转换为JSON对象   
           var tab = document.getElementById("table_id");//获取页面的table   
           for(var i=0;i<data.length;i++){   
                    var newLine = tab.insertRow();//添加一行   
                       var newCell1 = newLine.insertCell();   
                    var newCell2 = newLine.insertCell();   
                    newCell1.innerHTML=data[i].username;   
                    newCell2.innerHTML=data[i].age;   
         }   
}  


<html>
<head>
<title>test</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="GENERATOR"
    content="Rational® Application Developer™ for WebSphere® Software">
<script type="text/javascript">
    
var currentActiveRow; 
function   changeActiveRow(obj)   
{   
if(currentActiveRow)   currentActiveRow.style.backgroundColor="";   
currentActiveRow=obj;   
currentActiveRow.style.backgroundColor="gray"; 
document.getElementById("roleName").value=currentActiveRow.cells[0].innerText; 
document.getElementById("roleType").value=currentActiveRow.cells[1].innerText; 
} 

    </script>
</head>
<body>

<table border="1" id="mytable">
    <tbody>
        <tr>
            <td width="332">roleName</td>
            <td width="156">roleType</td>
        </tr>
        <tr onclick="changeActiveRow(this)">
            <td width="332">name1</td>
            <td width="156">type1</td>
        </tr>
        <tr onclick="changeActiveRow(this)">
            <td width="332">name2</td>
            <td width="156">type2</td>
        </tr>
    </tbody>
</table>
<hr>
roleName:
<input type="text" id="roleName">
<br>
roleType:
<input type="text" id="roleType">
<br>
</body>
</html>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics