js中点击按钮时自动增加一行

JavaScript010

js中点击按钮时自动增加一行,第1张

1、新建一个html文件,命名为test.html。

2、在test.html文件里,创建一个button按钮,并设置其id属性为dz,用于下面获得按钮对象,实现js点击按钮事件。

3、给button绑定点击事件,当按钮被点击时,执行hs()函数。

4、在test.html文件的js标签内,定义hs()函数,在函数内使用alert() 方法弹出"我被点击了"的内容。

5、在js内,使用window对象中的onload事件,当网页加载完时,执行function函数。

6、在function函数内,通过getElementById()方法获得按钮对象,使用click()方法点击按钮。

7、在浏览器打开test.html文件,当打开网页时,js自动执行点击事件。

您好!很高兴为您解答:

首先给‘增加项目’按钮绑定一个点击事件;

写一个函数,增加里面 的td即可;

举个小栗子:

<style>

div{width: 300px height: 100px background: red}

</style>

<input type="button" id="btn" />

<div></div>

<script>

var btn=document.getElementById('btn')

var div=document.getElementsByTagName('div')[0]

btn.onclick=function(){

div.innerHTML+="<div></div>"

}

</script>

望采纳

js如何在table中插入一行的方法是定位,插入:

1、通过id定位到myTable对象

var table = document.getElementById("myTable")

2、创建一个空的<tr>标签,并且插入到第一行:

var row = table.insertRow(0)

3、插入两列:

var cell1 = row.insertCell(0)

var cell2 = row.insertCell(1)

4、给列分别赋值:

cell1.innerHTML = "NEW CELL1"

cell2.innerHTML = "NEW CELL2"