JS和CSS的交互


一、样式的基本语法

  1. 标签选择器
  2. ID选择器
  3. 类选择器

1.1常见样式

在这里插入图片描述

1.2样式表类型

  1. 行内样式
  2. 内部样式表
  3. 外部样式表

二、JS与CSS交互案例

//使用style 改变样式
<head>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<title></title>
		<style type="text/css">
			li{
				font-size: 12px;
				color: red;
				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>资讯动态</li>
	<li>产品世界</li>
	<li>市场营销</li>
	</ul>
	<script type="text/javascript">
	var len=document.getElementsByTagName("li");
	for(var i=0;i<len.length;i++){
		len[i].onmouseover=function(){
			this.style.backgroundImage="url(img/bg2.gif)";
			}
		len[i].onmouseout=function(){
			this.style.backgroundImage="url(img/bg1.gif)";
			}	
			
		}
	
	</script>
	</body>

//使用className改变样式
	<title>使用classname改变样式</title>
		<style type="text/css">
		li{
			font-size: 12px;
			color: #ffffff;
			background-image: url(images/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(images/bg1.gif);
			}
		.over{
			background-image: url(images/bg2.gif);
			color:#ffff00;
			font-weight:bold;
			cursor:hand;
			}
		</style>
	</head>
	<body>
		<ul>
			<li onmouseover="this.style.backgroundImage='url(img/bg1.gif)'" onmouseout="this.style.backgroundImage='url(images/bg1.gif)'">资讯动态</li>
		    <li onmouseover="this.style.backgroundImage='url(img/bg1.gif)'" onmouseout="this.style.backgroundImage='url(images/bg1.gif)'">产品世界</li>
		    <li onmouseover="this.style.backgroundImage='url(img/bg1.gif)'" onmouseout="this.style.backgroundImage='url(images/bg1.gif)'">市场营销</li>
		
		</ul>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值