js中怎样设置动态tr的属性?

js中怎样设置动态tr的属性?,第1张

两个insert方法返回的就是被插入的对象,直接 *** 作这两个对象就行了。

var tr = table.insertRow()var td = tr.insertCell()tr.setAttribute('align','center') '不用setAttribute而直接使用属性也可以.tr.setAttribute('bgcolor','#FF0000')

td.width=100  直接对属性赋值的作法。

要在tr元素的父元素中才能增加行,不能在tr本身去增加行。而tbody就是tr的父元素。如果是增加td,就要用tr,因为tr是td的父元素。

<script>// Last updated 2006-02-21 function addRowToTable() { var tbl = document.getElementById('tblSample')var lastRow = tbl.rows.length。

// if there's no header row in the table, then iteration = lastRow + 1

var iteration = lastRow。

var row = tbl.insertRow(lastRow)// left cell var cellLeft = row.insertCell(0)var textNode = document.createTextNode(iteration)cellLeft.appendChild(textNode)

// right cell var cellRight = row.insertCell。

var el = document.createElement('input')el.type = 'text'el.name = 'txtRow' + iterationel.id = 'txtRow' + iterationel.size = 40。

el.onkeypress = keyPressTestcellRight.appendChild(el)。

<!DOCTYPE html>

<html lang="en-US">

<head>

<meta http-equiv="Content-Type" content="text/html charset=utf-8">

<title>testing</title>

<style type="text/css">

</style>

<script type="text/javascript">

window.onload = function ()

    {

    var table = document.getElementsByTagName ("table")[0]

    table.insertBefore(document.createElement('tbody'), table.tBodies[0])

    table.appendChild(document.createElement('tbody'))

    }

</script>

</head>

<body>

<table>

<tr>

<td></td>

</tr>

<tr>

<td></td>

</tr>

<tr>

<td></td>

</tr>

</table>

</body>

</html>


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11668671.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-17
下一篇 2023-05-17

发表评论

登录后才能评论

评论列表(0条)

保存