博主信息
博文 28
粉丝 1
评论 0
访问量 21573
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
2019.5.9作业
关超的博客
原创
773人浏览过
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>简单留言本</title>
</head>
<body>
<label>请输入内容</label>
<input type="text" id="comment" autofocus="autofocus" onkeydown="addComment(this)"/>

<ul id="list">	
</ul>
<script type="text/livescript">
	function addComment(comment){
			if(event.keyCode==13){
				var item = document.createElement('li');
				item.innerHTML=comment.value;
				item.innerHTML += ' &nbsp; <button  onclick="del(this)">删除</button>';
				
				var list = document.getElementById('list');
				if(list.childElementCount === 0){
						list.appendChild(item);
					}else{
						var first = list.firstElementChild;
						list.insertBefore(item,first);
						}			
				comment.value='';			
			}		
		}		
		function del(ele){
				return confirm("确定要删除吗?")?ele.parentElement.remove():false;
			}
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>js操作表格的基本技巧</title>
    <style>
        table,th,td{
            border: 1px solid #666;
        }
        table {
            width: 500px;
            text-align: center;
            border-collapse: collapse;
        }
        table caption {
            font-size: 1.2rem;
            margin-bottom: 15px;
        }
        /* 这里必须在nth-of-type(1)前添加父元素,否则thead,tbody中的第一行都会生效 */
        thead tr:nth-of-type(1) {
            background-color: lightblue;
        }

    </style>
</head>
<body>
   <table id="cart">
        <caption>购物车</caption>
        <thead>
            <tr>
                <th>编号</th>
                <th>品名</th>
                <th>数量</th>
                <th>单价</th>
            </tr>
        </thead>   
        <tbody></tbody>
    </table>

    <hr>
    
    <table id="cart2">
        <caption>购物车2</caption>
        <thead>
            <tr>
                <th>编号</th>
                <th>品名</th>
                <th>数量</th>
                <th>单价</th>
            </tr>
        </thead>   
        <tbody></tbody>
    </table>

    <script>
        
        var data = [
            {id: 1, name: '牛奶', count: 3, price: 50},
            {id: 1, name: '苹果', count: 10, price: 80},
            {id: 1, name: '衣服', count: 2, price: 600}
        ];

        var cart = document.getElementById('cart');
        var tbody = cart.children[2]; 
        data.forEach(function (value){
            var tr = document.createElement('tr');
            tr.innerHTML = '<td>' + value.id + '</td>';
            tr.innerHTML += '<td>' + value.name + '</td>';
            tr.innerHTML += '<td>' + value.count + '</td>';
            tr.innerHTML += '<td>' + value.price + '</td>';
            tbody.appendChild(tr);
        });

        

        var cart3 = document.getElementById('cart2');
        var tbody = cart2.tBodies[0];
        for (var i = 0; i < data.length; i++) {
            var tr = document.createElement('tr');
            Object.keys(data[i]).forEach(function(key){
                tr.innerHTML += '<td>' + data[i][key] + '</td>';
            });
            tbody.appendChild(tr);   
        }
    </script>
</body>
</html>


本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学