body onload()事件和table insertRow()、tr insertCell()

onload事件:css

定義和用法:html

onload 事件會在頁面或圖像加載完成後當即發生。函數

onload 一般用於 <body> 元素,在頁面徹底載入後(包括圖片、css文件等等。)執行腳本代碼。spa

語法:code

在HTML中:<body onload="SomeJavaScriptCode">htm

在JavaScropt中:window.onload=function(){SomeJavaScriptCode};blog

SomeJavaScriptCode必需。規定該事件發生時執行的 JavaScript。(執行代碼)事件

實例:<body onload="myFunction()">圖片

 

table insertRow()函數方法ip

定義和用法:insertRow() 方法用於在表格中的指定位置插入一個新行。

語法:tableObject.insertRow(index);index:指定插入新行的位置 (以 0 開始)。

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title></title> 
<script>
function displayResult(){
    var table=document.getElementById("myTable");
    var row=table.insertRow(0);
    var cell1=row.insertCell(0);
    var cell2=row.insertCell(1);
    cell1.innerHTML="New";
    cell2.innerHTML="New";
}
</script>
</head>
<body>

<table id="myTable" border="1">
    <tr>
        <td>cell 1</td>
        <td>cell 2</td>
    </tr>
    <tr>
        <td>cell 3</td>
        <td>cell 4</td>
    </tr>
</table>
<br>
<button type="button" onclick="displayResult()">插入新行</button>

</body>
</html>

 

 

tr insertCell()函數方法

定義和用法:insertCell() 方法用於在 HTML 表的一行的指定位置插入一個空的 <td> 元素。

語法:trObject.insertCell(index);index:該方法將建立一個新的 <td> 元素,把它插入行中指定的位置。新單元格將被插入當前位於 index 指定位置的表元以前。若是 index 等於行中的單元格數,則新單元格被附加在行的末尾。

實例:

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title></title> 
<script>
function displayResult(){
    var firstRow=document.getElementById("myTable").rows[0];
    var x=firstRow.insertCell(-1);
    x.innerHTML="New cell"
}
</script>
</head>
<body>

<table id="myTable" border="1">
    <tr>
        <td>First cell</td>
        <td>Second cell</td>
        <td>Third cell</td>
    </tr>
</table>
<br>
<button type="button" onclick="displayResult()">插入單元格</button>

</body>
</html>
相關文章
相關標籤/搜索