JS应用案例:轮播图,评论,表格隔行变色

评论案例:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style>
				
			textarea{
				width: 400px;
				height: 100px;
			}
			button {
				position: relative;
				left: 340px;
				top: 5px;
				border: 1px solid black;
				border-radius: 80px;
			}
			li{
				width: 370px;
				text-decoration: none;
				border: 1px solid #ccc;
				border-radius: 30px;
			}
		</style>
	</head>
	<body>
		<textarea id="text">请输入你的评论</textarea>
		<br />
		<button id="btn">发表评论</button>
		
		<ul id="utx">

		</ul>
	</body>
	
	<script>
        var text = document.getElementById("text");
        var btn = document.getElementById("btn");
        var utx = document.getElementById("utx");

        btn.onclick = function(){
            var creatLiEle=document.createElement('li');
			utx.insertBefore(creatLiEle,utx.firstElementChild);
            creatLiEle.innerHTML = text.value + "<a href='javascript:;'>删除</a>"
            text.value = ''

            var del = document.querySelectorAll('a')
            for (var i = 0; i < del.length; i++) {
                del[i].onclick = function () {
                    utx.removeChild(this.parentNode);
                }
            }
        }

        
		
	</script>
</html>

轮播图案例(只实现图片转换): 

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style>
			div {
				width: 590px;
				height: 470px;
				background-color: #ccc;
				margin: 50px auto;
				position: relative;

			}

			img {
				width: 590px;
				height: 470px;

			}

			a {
				display: block;
				width: 25px;
				height: 34px;
				text-align: center;
				line-height: 34px;
				text-decoration: none;
				color: #fff;
				background-color: burlywood;
			}

			a:nth-of-type(1) {
				position: absolute;
				top: 218px;
				border-radius: 0 50% 50% 0;
			}

			a:nth-of-type(2) {
				position: absolute;
				top: 218px;
				right: 0;
				border-radius: 50% 0 0 50%;
			}

			ul li {
				list-style: none;
				display: inline-block;
				background-color: #fff;
				width: 9.97px;
				height: 9.97px;
				border: 1px solid #fff;
				border-radius: 50%;

				/* text-align: center;
				line-height: 25px; */
			}

			ul {
				position: absolute;
				bottom: 5px;
				left: 150px;
			}
		</style>
	</head>
	<body>
		<div>
			<img src="img/car1.jpg" alt="">
			<!-- <img src="img/guoqing02.jpeg" alt="">
			<img src="img/guoqing03.jpeg" alt=""> -->
			<a href="#">&lt;</a>
			<a href="#">&gt;</a>
			<ul>
				<li></li>
				<li></li>
				<li></li>
				<li></li>
				<li></li>
			</ul>
		</div>
		<script>
			var number = 1;

			function run() {
				number++;
				if (number > 3) {
					number = 1;
				}
				var img = document.querySelector("img");
				img.src = "img/car" + number + ".jpg";
			}
			setInterval(run, 3000);
		</script>
	</body>
</html>

 

 

 

 

表格隔行变色案例:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<table border="1px" cellspacing = '0' width = '500px' height='300px'>
			<tr>
				<td>1-1</td>
				<td>1-1</td>
				<td>1-1</td>
				<td>1-1</td>
				<td>1-1</td>
			</tr>
			<tr>
				<td>1-1</td>
				<td>1-1</td>
				<td>1-1</td>
				<td>1-1</td>
				<td>1-1</td>
			</tr>
			<tr>
				<td>1-1</td>
				<td>1-1</td>
				<td>1-1</td>
				<td>1-1</td>
				<td>1-1</td>
			</tr>
			<tr>
				<td>1-1</td>
				<td>1-1</td>
				<td>1-1</td>
				<td>1-1</td>
				<td>1-1</td>
			</tr>
			<tr>
				<td>1-1</td>
				<td>1-1</td>
				<td>1-1</td>
				<td>1-1</td>
				<td>1-1</td>
			</tr>
			<tr>
				<td>1-1</td>
				<td>1-1</td>
				<td>1-1</td>
				<td>1-1</td>
				<td>1-1</td>
			</tr>
			<tr>
				<td>1-1</td>
				<td>1-1</td>
				<td>1-1</td>
				<td>1-1</td>
				<td>1-1</td>
			</tr>
			<tr>
				<td>1-1</td>
				<td>1-1</td>
				<td>1-1</td>
				<td>1-1</td>
				<td>1-1</td>
			</tr>
			<tr>
				<td>1-1</td>
				<td>1-1</td>
				<td>1-1</td>
				<td>1-1</td>
				<td>1-1</td>
			</tr>
			<tr>
				<td>1-1</td>
				<td>1-1</td>
				<td>1-1</td>
				<td>1-1</td>
				<td>1-1</td>
			</tr>
		</table>
		
		<script>
			//获取所有的tr标签
			var trEles = document.querySelectorAll('tr');
			//从trEles集合中取出元素,下标为偶数的元素表示的是奇数行,下标为奇数的元素表示的是偶数行
			for(var i =0;i<trEles.length;i++){
				if(i%2==0){
					trEles[i].style.backgroundColor = '#ccc';
				}else{
					trEles[i].style.backgroundColor = '#f00';
				}
			}
			
			
			
		</script>
	</body>
</html>

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值