- 9月 07 週四 201720:37
色碼記憶法
- 9月 07 週四 201714:50
過濾器filter、is、has、:text選取器
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<style type="text/css">
li{
width: 300px;
padding: 20px;
background-color: #eee;
list-style: none;
margin: 10px;
}
</style>
</head>
<body>
<ul>
<li class="one"><span>1</span></li>
<li class="two">2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
<script type="text/javascript">
$('li').filter(':last').css('background-color','#00f');
$('li').each(function(){
$(this).has('span').css('background-color','#0f0');
/*不會傳回布林值*/
if($(this).is('.two')){
$(this).css('background-color','#f00');
}
});
</script>
</body>
</html>
- 9月 07 週四 201714:50
過濾器filter、is、has、:text選取器
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<style type="text/css">
li{
width: 300px;
padding: 20px;
background-color: #eee;
list-style: none;
margin: 10px;
}
</style>
</head>
<body>
<ul>
<li class="one"><span>1</span></li>
<li class="two">2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
<script type="text/javascript">
$('li').filter(':last').css('background-color','#00f');
$('li').each(function(){
$(this).has('span').css('background-color','#0f0');
/*不會傳回布林值*/
if($(this).is('.two')){
$(this).css('background-color','#f00');
}
});
</script>
</body>
</html>
- 9月 07 週四 201714:30
find、closest、parent、parents、siblings、next、nextAll、prev、prevAll等DOM元素操作
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<style type="text/css">
.grandfather{
background-color: #eee;
display: inline-block;
}
.Afather,.Bfather{
background-color: #aaa;
display: inline-block;
margin: 10px;
}
.Achildren,.Bchildren{
display: inline-block;
width: 100px;
margin: 10px;
background-color: #555;
height: 100px;
}
button{
margin: 10px;
font-size: 25px;
}
.beselected{
background-color: #f59;
}
.main{
background-color: #0af;
}
</style>
</head>
<body>
<h2>A家庭</h2>
<div class="grandfather">
<div class="Afather father">
<div class="Achildren child"></div>
<div class="Achildren child"></div>
<div class="Achildren child main"></div>
<div class="Achildren child"></div>
<div class="Achildren child"></div>
</div>
<div class="Bfather father">
<div class="Bchildren child"></div>
<div class="Bchildren child"></div>
<div class="Bchildren child"></div>
</div>
</div>
<br>
<button id="reset">清空</button>
<button id="findchild">Afather.find('Achildren')</button>
<button id="closest">Achildren.closest('father')</button><br>
<button id="parent">Achildren.parent()</button>
<button id="parents">Achildren.parents()</button>
<button id="siblings">Achildren.siblings()</button><br>
<button id="next">Achildren.next()</button>
<button id="nextAll">Achildren.nextAll()</button>
<button id="prev">Achildren.prev()</button>
<button id="prevAll">Achildren.prevAll()</button>
<script type="text/javascript">
$('#reset').on('click',function(){
$('*').removeClass('beselected');
});
$('#findchild').on('click',function(){
$('.Afather').find('.Achildren').addClass('beselected');
});
$('#closest').on('click',function(){
$('.main').closest('.father').addClass('beselected');
});
$('#parent').on('click',function(){
$('.main').parent().addClass('beselected');
});
$('#parents').on('click',function(){
$('.main').parents().addClass('beselected');
});
$('#siblings').on('click',function(){
$('.main').siblings().addClass('beselected');
});
$('#next').on('click',function(){
$('.main').next().addClass('beselected');
});
$('#nextAll').on('click',function(){
$('.main').nextAll().addClass('beselected');
});
$('#prev').on('click',function(){
$('.main').prev().addClass('beselected');
});
$('#prevAll').on('click',function(){
$('.main').prevAll().addClass('beselected');
});
</script>
</body>
</html>
- 9月 07 週四 201714:30
find、closest、parent、parents、siblings、next、nextAll、prev、prevAll等DOM元素操作
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<style type="text/css">
.grandfather{
background-color: #eee;
display: inline-block;
}
.Afather,.Bfather{
background-color: #aaa;
display: inline-block;
margin: 10px;
}
.Achildren,.Bchildren{
display: inline-block;
width: 100px;
margin: 10px;
background-color: #555;
height: 100px;
}
button{
margin: 10px;
font-size: 25px;
}
.beselected{
background-color: #f59;
}
.main{
background-color: #0af;
}
</style>
</head>
<body>
<h2>A家庭</h2>
<div class="grandfather">
<div class="Afather father">
<div class="Achildren child"></div>
<div class="Achildren child"></div>
<div class="Achildren child main"></div>
<div class="Achildren child"></div>
<div class="Achildren child"></div>
</div>
<div class="Bfather father">
<div class="Bchildren child"></div>
<div class="Bchildren child"></div>
<div class="Bchildren child"></div>
</div>
</div>
<br>
<button id="reset">清空</button>
<button id="findchild">Afather.find('Achildren')</button>
<button id="closest">Achildren.closest('father')</button><br>
<button id="parent">Achildren.parent()</button>
<button id="parents">Achildren.parents()</button>
<button id="siblings">Achildren.siblings()</button><br>
<button id="next">Achildren.next()</button>
<button id="nextAll">Achildren.nextAll()</button>
<button id="prev">Achildren.prev()</button>
<button id="prevAll">Achildren.prevAll()</button>
<script type="text/javascript">
$('#reset').on('click',function(){
$('*').removeClass('beselected');
});
$('#findchild').on('click',function(){
$('.Afather').find('.Achildren').addClass('beselected');
});
$('#closest').on('click',function(){
$('.main').closest('.father').addClass('beselected');
});
$('#parent').on('click',function(){
$('.main').parent().addClass('beselected');
});
$('#parents').on('click',function(){
$('.main').parents().addClass('beselected');
});
$('#siblings').on('click',function(){
$('.main').siblings().addClass('beselected');
});
$('#next').on('click',function(){
$('.main').next().addClass('beselected');
});
$('#nextAll').on('click',function(){
$('.main').nextAll().addClass('beselected');
});
$('#prev').on('click',function(){
$('.main').prev().addClass('beselected');
});
$('#prevAll').on('click',function(){
$('.main').prevAll().addClass('beselected');
});
</script>
</body>
</html>
- 9月 07 週四 201713:16
按下去後,往右淡出並刪掉,利用animate
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<style type="text/css">
li{
padding: 20px;
background-color: #0af;
margin: 10px 0;
list-style: none;
width: 300px;
}
</style>
</head>
<body>
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
<script type="text/javascript">
$('li').on('click',function(){
$(this).animate({
opacity:0,
'margin-left':'+=80'
},500,function(){
$(this).remove();
});
});
</script>
</body>
</html>
- 9月 07 週四 201713:16
按下去後,往右淡出並刪掉,利用animate
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<style type="text/css">
li{
padding: 20px;
background-color: #0af;
margin: 10px 0;
list-style: none;
width: 300px;
}
</style>
</head>
<body>
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
<script type="text/javascript">
$('li').on('click',function(){
$(this).animate({
opacity:0,
'margin-left':'+=80'
},500,function(){
$(this).remove();
});
});
</script>
</body>
</html>
- 9月 07 週四 201713:07
on綁定傳入四個參數,事件物件的type和data.自定義
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<style type="text/css">
#btn{
background-color: #0ef;
padding: 20px;
width: 300px;
}
li{
margin: 10px 0;
}
</style>
</head>
<body>
<ul id="btn"><li>按我</li> <li id="nono">按我沒反應喔</li></ul>
<div id="result"></div>
<script type="text/javascript">
$('#btn').on('click',':not(#nono)',{status:'被按下去囉'},function(e){
$('#result').html(
'<h3>按鈕上的文字:'+e.target.textContent+'</h3>'+
'<h3>按鈕的狀態:'+e.data.status+'</h3>'+
'<h3>按鈕綁定的事件:'+e.type+'</h3>'
);
});
</script>
</body>
</html>
- 9月 07 週四 201713:07
on綁定傳入四個參數,事件物件的type和data.自定義
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<style type="text/css">
#btn{
background-color: #0ef;
padding: 20px;
width: 300px;
}
li{
margin: 10px 0;
}
</style>
</head>
<body>
<ul id="btn"><li>按我</li> <li id="nono">按我沒反應喔</li></ul>
<div id="result"></div>
<script type="text/javascript">
$('#btn').on('click',':not(#nono)',{status:'被按下去囉'},function(e){
$('#result').html(
'<h3>按鈕上的文字:'+e.target.textContent+'</h3>'+
'<h3>按鈕的狀態:'+e.data.status+'</h3>'+
'<h3>按鈕綁定的事件:'+e.type+'</h3>'
);
});
</script>
</body>
</html>

