javascript和css的互换

这篇博客通过三个课堂案例展示了如何利用JavaScript和CSS动态地改变网页元素的样式。案例1中,通过className切换实现图片边框颜色变化;案例2中,利用onmouseover和onmouseout事件改变li元素的背景图像和文字颜色;案例3中,直接通过style属性修改li元素的背景图像。这些例子说明了JavaScript与CSS结合在交互式网页设计中的应用。
摘要由CSDN通过智能技术生成

样式表的基本语法

标签选择器:

input{
   width:120px;
   border:solid 1px #ff0000;
}

ID选择器:

#flow{color:#ff0000}

类选择器:

.center{
   text-align:center;
   font-weight:bold;
}

常见样式

课堂案例1:改变图片边框样式

	<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style type="text/css">
			/* 改变图片边框 */
			.sa{
				border: 2px solid red;
			}
			/* 恢复图片边框 */
			.sb{}
		</style>
		<!-- <script type="text/javascript"> -->
			<!-- // // 改变图片边框 -->
			<!-- // function changeImage(obj){ -->
			<!-- // 	obj.className="sa"; -->
			<!-- // } -->
			<!-- //  // 恢复图片边框 -->
			<!-- // function hf(obj){ -->
			<!-- // 	//获取图片 -->
			<!-- // 	obj.className="sb"; -->
			<!-- // }	 -->
		</script>
	</head>
	<body>
		<img onmouseleave="this.className='sb'" onmousemove="this.className='sa'" src="img/new_01.jpg" />
		<img onmouseleave="this.className='sb'" onmousemove="this.className='sa'" src="img/new_02.jpg" />
		<img onmouseleave="this.className='sb'" onmousemove="this.className='sa'" src="img/new_03.jpg" />
	</body>
</html>

课堂案例2:使用className改变样式表

	<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style type="text/css">
			li{
				font-size: 12px;
				color: #FFFFFF;
				background-image: url(img/bg1.gif);
				background-repeat: no-repeat;
				text-align: center;
				height: 33px;
				width: 104px;
				line-height: 38px;
				float: left;
				list-style: none;
			}
			.out{
				background-image: url(img/bg1.gif);
			}
			.over{
				background-image: url(img/bg2.gif);
				color: #ffff00;
				font-weight: bold;
				cursor: hand;
			}
		</style>
	</head>
	<body>
		<ul>
			<li onmousemove="this.className='over'" onmouseout="this.className='out'">资讯动态</li>
			<li onmousemove="this.className='over'" onmouseout="this.className='out'">产品世界</li>
			<li onmousemove="this.className='over'" onmouseout="this.className='out'">市场营销</li>
		</ul>
	</body>
</html>

课堂案例3:使用style改变样式表

	<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style type="text/css">
			li{
				font-size: 12px;
				color: #ffffff;
				background-image: url(img/bg1.gif);
				background-repeat: no-repeat;
				text-align: center;
				height: 33px;
				width: 104px;
				line-height: 38px;
				float: left;
				list-style: none;
			}
		</style>
	</head>
	<body>
		<ul>
			<li onmouseover="this.style.backgroundImage='url(images/bg2.gif)'" onmouseout="this.style.backgroundImage='url(images/bg1.gif)'">资讯动态</li>
			<li onmouseover="this.style.backgroundImage='url(images/bg2.gif)'" onmouseout="this.style.backgroundImage='url(images/bg1.gif)'">产品世界</li>
			<li onmouseover="this.style.backgroundImage='url(images/bg2.gif)'" onmouseout="this.style.backgroundImage='url(images/bg1.gif)'">市场营销</li>
		</ul>
	</body>
</html>`
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值