<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>jQuery选择器</title>
<style type="text/css">
#div1{
color: red;
}
.box{
color: green;
}
.list li{
margin-bottom: 10px;
}
</style>
<script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
('#div1').css({color: 'pink'});
('.list li').css({
background: 'green',
color: '#fff',
fontSize: '20px',
marginBottom: '10px'
});
})
</script>
</head>
<body>
<div id="div1">这是一个div元素</div>
<div class="box">这是第二个div元素</div>
<ul class="list">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>
<li>8</li>
</ul>
</body>
</html>
!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>jQuery样式操作</title>
<style type="text/css">
.div2{
color: red;
}
.big{
font-size: 30px;
}
</style>
<script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
('#div1').css('fontSize'));//16px
//设置(写入)样式
$('#div1').css({background:'gold'});
//增加行间样式
$('#div1').addClass('big');
//减少行间样式,多个样式用空格分开
$('#div1').removeClass('div2 big');
})
</script>
</head>
<body>
<div id="div1" class="div2">这是一个div元素</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>jQuery索引值</title>
<style type="text/css">
.list li{
height: 30px;
margin-bottom: 10px;
background-color: gold;
}
</style>
<script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
('.list li').click(function(){
// alert(this.innerHTML);//弹出标签中的内容
alert($(this).index());//弹出下标
})
})
</script>
</head>
<body>
<ul class="list">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>
<li>8</li>
</ul>
</body>
网友评论