jQuery屬性操作、迴圈
阿新 • • 發佈:2019-01-09
1.html()取出或設定html內容
2.prop()取出或設定某個屬性的值
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>屬性操作</title>
<script type="text/javascript" src="../jQuery庫/jquery-3.3.1.min.js"></script>
<script type="text/javascript">
$(function(){ var $a = $('.link'); var $img = $('#img'); /*取出某個屬性*/ console.log($a.prop('class')) /*絕對地址*/ console.log($img.prop('src')); /*設定屬性*/ $a.prop({'href':'http://www.baidu.com','title':'百度網'}); /*在標籤內寫內容*/ $a.html('<em>3</em>'); }) </script>
</head>
<body>
<a href="#" class="link">連結</a>
<img src="../../js/images/小林子1.jpg" alt="nanzi" id="img">
</body>
</html>
例子二:手風琴
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>手風琴</title>
<script type="text/javascript" src="../jQuery庫/jquery-3.3.1.min.js"></script>
<script typt="text/javascript">
$(function(){ var $li = $('.accordion li'); $li.click(function(){ /*點選的li,左的位置*/ $(this).animate({'left':21*$(this).index()}); /*點選的li前面元素也跟著左移*/ $(this).prevAll().each(function(){ $(this).animate({'left':21*$(this).index()}); }); /*點選的li後面的元素左的位置*/ $(this).nextAll().each(function(){ $(this).animate({'left':497-21*(5-$(this).index())}); }) }) }) </script>
<style type="text/css">
.accordion{
width: 497px;
height: 506px;
float: left;
overflow: hidden;
position: relative;
left: 50%;
margin-left: -250px;
}
.accordion ul{
list-style: none;
padding: 0;
}
.accordion ul li{
width: 413px;
height: 506px;
display: inline-block;
float: left;
}
.accordion li span{
display: inline-block;
width: 20px;
height: 506px;
border-bottom: 1px solid #fff;
}
.accordion li img{
display: inline-block;
}
/*位置一*/
.accordion .pick1{
position: absolute;
top: 0;
background-color: #EF4B4E;
}
.accordion .pick1 span{
position: absolute;
top: 0;
}
.accordion .pick1 img{
position: absolute;
top: 0;
left: 21px;
}
/*位置二*/
.accordion .pick2{
position: absolute;
top: 0;
left: 413px;
background-color: #EC3AF1;
}
.accordion .pick2 span{
position: absolute;
top: 0;
}
.accordion .pick2 img{
position: absolute;
left: 21px;
}
/*位置三*/
.accordion .pick3{
position: absolute;
top: 0;
left: 434px;
background-color: #4866F5;
}
.accordion .pick3 span{
position: absolute;
top: 0;
}
.accordion .pick3 img{
position: absolute;
left: 21px;
}
/*位置四*/
.accordion .pick4{
position: absolute;
top: 0;
left: 455px;
background-color: #4DC1F1;
}
.accordion .pick4 span{
position: absolute;
top: 0;
}
.accordion .pick4 img{
position: absolute;
left: 21px;
}
/*位置五*/
.accordion .pick5{
position: absolute;
top: 0;
left: 476px;
background-color: #4CF750;
}
.accordion .pick5 span{
position: absolute;
top: 0;
}
.accordion .pick5 img{
position: absolute;
left: 21px;
}
</style>
</head>
<body>
<div class="accordion">
<ul>
<li class="pick1">
<span>小南子01</span><img src="../../js/images/小林子1.jpg" alt="nanzi">
</li>
<li class="pick2">
<span>小南子02</span><img src="../../js/images/小林子2.png" alt="nanzi">
</li>
<li class="pick3">
<span>小南子03</span><img src="../../js/images/小林子3.png" alt="nanzi">
</li>
<li class="pick4">
<span>小南子04</span><img src="../../js/images/小林子4.png" alt="nanzi">
</li>
<li class="pick5">
<span>小南子05</span><img src="../../js/images/小林子5.png" alt="nanzi">
</li>
</ul>
</div>
</body>
</html>
3.jQuery迴圈
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>迴圈</title>
<script type="text/javascript" src="../jQuery庫/jquery-3.3.1.min.js"></script>
<script type="text/javascript">
$(function(a){
var $li = $('ul li');
/*迴圈each,獲取每一個li*/
$li.each(function(){
if($(this).index()%2==0){
$(this).css({'backgroundColor':'gold'});
}
})
})
</script>
</head>
<body>
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
</ul>
</body>
</html>