博主信息
博文 11
粉丝 0
评论 0
访问量 7720
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
1.17作业
阿力的博客
原创
721人浏览过

实例

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<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>
	<body>
		  <table id="cart1">
        <caption>购物车1</caption>
        <!-- 注: 浏览器会自动给以下内容添加一个tbody容器 -->
        <!-- 为防止作用js获取元素出错,我们手工添加上这个tbody -->
        <!-- 将表头手工加上thead,否则会被添加二个tbody,因为table允许使用多个tbody -->
        <thead>
            <tr>
                <th>编号</th>
                <th>品名</th>
                <th>数量</th>
                <th>单价</th>
            </tr>
        </thead>
        
        <tbody>
            <tr>
                <td>1</td>
                <td>手机</td>
                <td>1</td>
                <td>3000</td>
            </tr>
            <tr>
                <td>2</td>
                <td>电脑</td>
                <td>1</td>
                <td>5000</td>
            </tr>
            <tr>
                <td>3</td>
                <td>手机</td>
                <td>1</td>
                <td>3000</td>
            </tr>
        </tbody>    
    </table>

    <hr>

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

    <hr>
    
    <table id="cart3">
        <caption>购物车3</caption>
        <thead>
            <tr>
                <th>编号</th>
                <th>品名</th>
                <th>数量</th>
                <th>单价</th>
            </tr>
        </thead>   
        <tbody></tbody>
    </table>
	</body>
	<script>
	var cart1 = document.getElementById('cart1');//获取cart1结构
    
	 console.log(cart1.children[2].children[1].children[1].innerHTML)
	 console.log(cart1.children[2].children[1].children[3].innerHTML)
	 console.log(cart1.tBodies[0].rows[1].cells[3].innerHTML);
     
		
		
		
	</script>
	<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 cart2 =document.getElementById('cart2');//获取这个购物车结构
		var tbody =  cart2.children[2];//获取tbody
		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);

		})
			
			
		
		
		
		
	</script>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例

实例

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		
		 <h3>请留言</h3>
    <input type="text"><!--获取表单-->
     <ul></ul>

		
		
		
		
	</body>
	<script>
	  var comment = document.querySelector('input');
	  var ul = document.getElementsByTagName('ul').item(0);
	  comment.focus();
	  comment.onkeydown=function(event){
		  if(event.keyCode===13){
		var li= document.createElement("li");
		 li.innerHTML = comment.value + '<a href="javascript:;" onclick="del(this)">删除</a>';
		if(ul.childrenElementCount===0){
			ul.appendChild(li);
		}else{
			
			var first =ul.firstElementChild;
			ul.insertBefore(li,first);
		}
		 comment.value = '';
            comment.focus();
		  }
		  
		  
		  
		  
	  }
	
		
		
	</script>
	
	
</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+教程免费学