input框事件
$(function(){
// //一開始就獲取焦點(diǎn),相當(dāng)于設(shè)置了autofocus自動(dòng)獲取焦點(diǎn)了(HTML5 新增表單控件屬性)
$('#txt01').focus();
// //文本框獲取焦點(diǎn)的時(shí)候(有光標(biāo)閃爍的時(shí)候)
$('#txt01').focus(function() {
alert('focus');
});
// //失去焦點(diǎn)的時(shí)候(光標(biāo)離開的時(shí)候)
$('#txt01').blur(function() {
alert('blur');
});
// //輸入框內(nèi)容發(fā)生變化的時(shí)候,失去焦點(diǎn)后觸發(fā),可用于注冊(cè)時(shí)驗(yàn)證用戶名是否已存在
$('#txt01').change(function() {
alert('change');
});
//松開鍵盤按鍵就觸發(fā)
$('#txt01').keyup(function() {
alert('keyup');
});
})
jQuery屬性操作
$(function(){
/*
alert($('.box').html());//這是一個(gè)div元素
$('.box').html('<a );
*/
/*
讀寫值為布爾類型的屬性用prop方法
讀寫值為非布爾類型的屬性用attr方法
*/
/*
$('.box').attr({title:'這是一個(gè)div!'});//寫入title屬性,并賦值
alert($('.box').attr('class'));//讀屬性class的值,彈出box
*/
/*
var $src = $('#img1').attr('src');
alert($src);//img/1.png
$('#img1').attr({
src:'img/2.gif',
alt:'圖片二'
});
*/
/*
alert($('#check').prop('checked'));//選中為true,非選中為false
$('#check').prop({checked:true});//設(shè)置默認(rèn)勾選
*/
// alert($('.box2').html());//<span>這是div元素內(nèi)的span</span>
alert($('.box2').text());//這是div元素內(nèi)的span
})
特殊效果
.box{
width: 200px;
height: 200px;
background-color: gold;
display: none;
}
</style>
<script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
$(function(){
$('#btn').click(function(){
// $('.box').fadeOut();//淡出
// $('.box').fadeIn();//淡入
// $('.box').fadeToggle();//切換淡入淡出
// $('.box').toggle();//切換顯示隱藏
$('.box').slideToggle();//切換上收和下展
})
})
動(dòng)畫
.box{
width: 100px;
height: 100px;
background-color: gold;
}
</style>
<script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
$(function(){
/*
參數(shù):
1、什么屬性做動(dòng)畫,屬性設(shè)置{param1: value1, param2: value2}
2、動(dòng)畫執(zhí)行的時(shí)間,單位毫秒
3、動(dòng)畫曲線:
swing(默認(rèn)值)開始和結(jié)束慢,中間快
linear勻速
可省略不寫
4、回調(diào)函數(shù),動(dòng)畫完成之后要做的事情,可無限嵌套
*/
$('#div1').animate({
width: 200,
height: 200},
1000,
function(){
// alert('動(dòng)畫完了!');
$(this).animate(
{marginLeft: 500},
1000,
function(){
$(this).animate(
{marginTop: 500},
1000
)
}
)
}
);
})
循環(huán)
$(function(){
// //給全部的li設(shè)置內(nèi)容和樣式
// $('.list li').html('111');
// $('.list li').css({background:'gold'});
//第一個(gè)參數(shù)index是索引值
$('.list li').each(function(index) {
// alert(index);//彈出索引值
//$(this)是每一個(gè)li
$(this).html(index);
});
})
元素絕對(duì)位置
.con{
width: 600px;
height: 600px;
margin: 50px auto 0;
}
.box{
width: 100px;
height: 100px;
background-color: gold;
margin-bottom: 10px;
}
.pos{
margin-left: 500px;
}
.pop{
width: 100px;
height: 100px;
background-color: red;
position: fixed;
left:0;
top: 0;
display: none;
}
</style>
<script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
$(function(){
var $pos = $('.pos');
//offset()是獲取相對(duì)于頁面左上角的絕對(duì)位置,即使外面再包一層con居中層,也不影響效果
var pos = $pos.offset();
// console.log(pos);
// alert(pos.left + "," + pos.top);
var w = $pos.outerWidth();
var h = $pos.outerHeight();
// alert(w);
$('.pop').css({left:pos.left + w,top:pos.top});
$pos.mouseover(function() {
$('.pop').show();
});
$pos.mouseout(function() {
$('.pop').hide();
});
})
鼠標(biāo)移入移出
.box{
width: 200px;
height: 200px;
background-color: gold;
margin: 100px auto 0;
}
.son{
width: 100px;
height: 100px;
background-color: green;
}
</style>
<script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
$(function(){
/*進(jìn)入子元素也觸發(fā)*/
/*$('#div1').mouseover(function() {
$(this).animate({marginTop: 50});//.stop()
});
$('#div1').mouseout(function() {
$(this).animate({marginTop: 100});//.stop()
});*/
/*進(jìn)入子元素不觸發(fā)*/
/*$('#div1').mouseenter(function() {
$(this).stop().animate({marginTop: 50});//
});
$('#div1').mouseleave(function() {
$(this).stop().animate({marginTop: 100});//
});*/
/*通過hover(mouseenter+mouseleave)實(shí)現(xiàn)簡(jiǎn)寫*/
$('#div1').hover(function() {
$(this).stop().animate({marginTop: 50});
}, function() {
$(this).stop().animate({marginTop: 100});
});
})
其他事件
// //JS原生寫法
// window.onload = function(){
// }
// //jQuery寫法,等同于上面寫法
// $(window).load(function(){
// })
// //ready的寫法
// $(document).ready(function(){
// })
// //ready的簡(jiǎn)寫
// $(function(){
// })
// 窗口改變尺寸的時(shí)候,會(huì)高頻觸發(fā)
$(window).resize(function() {
console.log('3');
});
綁定事件bind
$(function(){
// //只能綁定click事件,不能綁定其他的了
// $('#btn').click(function() {
// /* Act on the event */
// });
//bind方式可綁定多個(gè)事件
$('#btn').bind('click mouseover', function() {
alert('hello!');
//取消綁定事件
$(this).unbind('mouseover');
});
})
自定義事件
$(function(){
//自定義事件只能用bind方式綁定,第一個(gè)參數(shù)是事件的名字,第二個(gè)參數(shù)是事件發(fā)生時(shí)執(zhí)行的函數(shù)
$('#btn1').bind('hello', function(){
alert('hello');
})
$('#btn1').bind('click', function(){
alert('click');
})
$('#btn2').click(function() {
// trigger即可以觸發(fā)自定義事件,也可以觸發(fā)原始的事件
$('#btn1').trigger('hello');
$('#btn1').trigger('click');
});
//不一定點(diǎn)擊按鈕觸發(fā),也可頁面加載時(shí)觸發(fā),也可在滿足某種if條件時(shí)觸發(fā)
// $('#btn1').trigger('hello');
})
事件冒泡
.grandfather{
width: 300px;
height: 300px;
background-color: green;
position: relative;
}
.father{
width: 200px;
height: 200px;
background-color: gold;
}
.son{
width: 100px;
height: 100px;
background-color: red;
position: absolute;
left: 0;
top: 400px;
}
</style>
<script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
$(function(){
$('body').click(function() {
alert(4);
});
$('.grandfather').click(function() {
alert(3);
});
$('.father').click(function() {
alert(2);
});
$('.son').click(function(event) {//event代表當(dāng)前事件
alert(1);
// console.log(event);//顯示很多屬性,其中clientX、clientY就是點(diǎn)擊的坐標(biāo)
// alert("X軸坐標(biāo):" + event.clientX);
// //阻止事件冒泡
// event.stopPropagation();
//合并阻止操作:把阻止冒泡和阻止默認(rèn)行為合并
return false;
});
//阻止右鍵菜單
$(document).contextmenu(function(event){
// //阻止默認(rèn)行為(原來右鍵能彈出菜單,阻止后無法彈出)
// event.preventDefault();
//合并阻止
return false;
})
})
</script>
</head>
<body>
<div class="grandfather">
<div class="father">
<div class="son"></div>
</div>
</div>
定時(shí)器彈框
.pop_con{
display: none;/*默認(rèn)不顯示,用定時(shí)器顯示*/
}
.pop{
width: 400px;
height: 300px;
background-color: #fff;
border: 1px solid #000;
position: fixed;/*使用固定定位*/
left: 50%;/*左上角位于頁面中心*/
top: 50%;
margin-left: -200px;/*讓div向左偏移半個(gè)寬度、向上偏移半個(gè)高度,使div位于頁面中心*/
margin-top: -150px;
z-index: 9999;/*彈窗在最前面*/
}
/*遮罩樣式*/
.mask{
position: fixed;
width: 100%;
height: 100%;
background-color: #000;
left: 0;
top: 0;
/*設(shè)置透明度30%,兼容IE6、7、8*/
opacity: 0.3;
filter: alpha(opacity=30);
z-index: 9990;/*遮罩在彈窗后面*/
}
</style>
<script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
$(function(){
$('#btn').click(function() {
$('#pop').show();
return false;
});
$('#shutoff').click(function() {
$('#pop').hide();
});
//點(diǎn)彈框以外的地方,也能讓彈框消失
$(document).click(function(){
// setTimeout(function(){
// $('#pop').hide();
// },2000);
$('#pop').hide();
});
$('.pop').click(function() {
return false;
});
//阻止默認(rèn)行為(原來超鏈接可跳轉(zhuǎn)到百度,阻止后無法跳轉(zhuǎn))
$('#link1').click(function() {
return false;
});
})
</script>
</head>
<body>
<h1>首頁標(biāo)題</h1>
<p>頁面內(nèi)容</p>
<a id="link1">百度網(wǎng)</a>
<input type="button" name="" value="彈出" id="btn">
<div class="pop_con" id="pop">
<div class="pop">
<h3>提示信息!</h3>
<a href="#" id="shutoff">關(guān)閉</a>
<input type="text" name="">
</div>
<!-- 遮罩層 -->
<div class="mask"></div>
</div>
事件委托
.list{
list-style: none;
}
.list li{
height: 30px;
background-color: green;
margin-bottom: 10px;
color: #fff;
}
</style>
<script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
$(function(){
/*
給每個(gè)li綁定事件,一共綁定了8次,性能不高
$('.list li').click(function() {
alert($(this).html());
});
*/
/*
事件委托:方法delegate,只綁定一次事件,冒泡觸發(fā)
參數(shù):
selector選擇器:寫入ul下面的所有要發(fā)生事件的元素,多個(gè)元素用空格隔開,例如‘li a span’
eventType事件
function要執(zhí)行的操作
$('.list').delegate('li', 'click', function() {
//$(this)指發(fā)生事件的子集,即每個(gè)li
alert($(this).html());
//全部取消委托
$('.list').undelegate();
});
})
節(jié)點(diǎn)操作
$(function(){
var $span = $('<span>span元素</span>');
var $p = $('<p>p段落元素</p>');
var $h = $('<h1>頁面標(biāo)題</h1>');
/*插入子元素*/
//div中插入span和p(末尾追加)
// $('#div1').append($span);
// $('#div1').append($p);
// 把span和p插入div中
$span.appendTo('#div1');
$p.appendTo('#div1');
//把子元素插入到父元素(前面追加)
// $('#div1').prepend($span);
// $('#div1').prepend($p);
// $span.prependTo('#div1');
// $p.prependTo('#div1');
//在div前邊插入兄弟h1標(biāo)題
// $('#div1').before($h);
$h.insertBefore('#div1');
//在后邊插入兄弟元素
//after()
//insertAfter()
//刪除p標(biāo)簽
$p.remove();
})