JQuery属性操作、特殊效果、事件、冒泡

jQuery属性操作

  • 读写值为布尔类型的属性用prop方法

    读取.box的属性,标签也显示
    $(function(){
    alert($('.box').html());///<span>这是div元素内的span</span>
    
    写入
    $('.box').html('<ahref="http://www.baidu.com">百度网</a>');
    
    
  • 读写值为非布尔类型的属性用attr方法

 $('.box').attr({title:'这是一个div!'});//写入title属性,并赋值
 $('.box').attr('class');//读属性class的值,弹出box 

  • 多选框用prop方法

    alert($('#check').prop('checked'));//选中为true,非选中为false
    $('#check').prop({checked:true});//设置默认勾选
    
    
  • 获取标签文本内容

    alert($('.box2').text());//这是div元素内的span
    
    

jQuery特殊效果

fadeIn() 淡入

$btn.click(function(){

    $('#div1').fadeIn(1000,'swing',function(){
        alert('done!');
    });

});

fadeOut() 淡出
fadeToggle() 切换淡入淡出
hide() 隐藏元素
show() 显示元素
toggle() 依次展示或隐藏某个元素
slideDown() 向下展开
slideUp() 向上卷起
slideToggle() 依次展开或卷起某个元素

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>jQuery特殊效果</title>
    <style type="text/css">
        .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();//切换上收和下展
            })
        })
    </script>
</head>
<body>
    <input type="button" name="" value="效果" id="btn">
    <div class="box"></div>
</body>
</html>

jQuery动画

参数:
1、什么属性做动画,属性设置{param1: value1, param2: value2}
2、动画执行的时间,单位毫秒
3、动画曲线:
swing(默认值)开始和结束慢,中间快
linear匀速
可省略不写
4、回调函数,动画完成之后要做的事情,可无限嵌套
jquery动画
通过animate方法可以设置元素某属性值上的动画,可以设置一个或多个属性值,动画执行完成后会执行一个函数。

$('#div1').animate({
    width:300,
    height:300
},1000,swing,function(){
    alert('done!');
});

参数可以写成数字表达式:

$('#div1').animate({
    width:'+=100',
    height:300
},1000,swing,function(){
    alert('done!');
});

例:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>jQuery动画</title>
    <style type="text/css">
        .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(){
            $('#div1').animate({
                width: 200,
                height: 200},
                1000,"linear"//linear匀速,默认是swing中间块,开始结束慢
                function(){//可以接着上一个动画结束后再进行动作
                    // alert('动画完了!');
                    $(this).animate(
                        {marginLeft: 500},
                        1000,
                        function(){
                            $(this).animate(
                                {marginTop: 500},
                                1000
                            )
                        }
                    )
                }
            );
        })
    </script>
</head>
<body>
    <div id="div1" class="box"></div>
</body>
</html>

jQuery循环

each循环

 <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>jQuery循环</title>
    <style type="text/css">

    </style>
    <script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
    <script type="text/javascript">
        $(function(){
            // //给全部的li设置内容和样式
             $('.list li').html('111');
             $('.list li').css({background:'gold'});

            //each循环
            $('.list li').each(function(index) {
                 alert(index);//弹出索引值
                //0,1,2,3,4,5,6,7,8

                //$(this)是每一个li
                $(this).html(index);
            });
        })
    </script>
</head>
<body>
    <ul class="list">
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
    </ul>
</body>
</html>

尺寸相关、滚动事件

1、获取和设置元素的尺寸

width()、height()    获取元素width和height  
innerWidth()、innerHeight()  包括padding的width和height  
outerWidth()、outerHeight()  包括padding和border的width和height  
outerWidth(true)、outerHeight(true)   包括padding和border以及margin的width和height

2、获取元素相对页面的绝对位置

offse()

3、获取可视区高度

$(window).height();

4、获取页面高度

$(document).height();

5、获取页面滚动距离

$(document).scrollTop();  
$(document).scrollLeft();

6、页面滚动事件

$(window).scroll(function(){  
......  
})

例:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>元素绝对位置</title>
    <style type="text/css">
        .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()是获取相对于页面左上角的绝对位置,即使外面再包一层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();
            });
        })
    </script>
</head>
<body>
    <div class="con">
        <div class="box"></div>
        <div class="box"></div>
        <div class="box pos"></div>
        <div class="box"></div>
    </div>

    <div class="pop">提示信息!</div>
</body>
</html>

jquery事件函数列表:

blur() 元素失去焦点
focus() 元素获得焦点
change() 表单元素的值发生变化
click() 鼠标单击
dblclick() 鼠标双击
mouseover() 鼠标进入(进入子元素也触发)
mouseout() 鼠标离开(离开子元素也触发)
mouseenter() 鼠标进入(进入子元素不触发)
mouseleave() 鼠标离开(离开子元素不触发)
hover() 同时为mouseenter和mouseleave事件指定处理函数

<script type="text/javascript">
        $(function(){
            /*进入子元素也触发*/
            $('#div1').mouseover(function() {
                $(this).animate({marginTop: 50});//.stop()
            });

            /*进入子元素不触发*/
            $('#div1').mouseenter(function() {
                $(this).stop().animate({marginTop: 50});//
            });

            /*通过hover(mouseenter+mouseleave)实现简写*/
            $('#div1').hover(function() {
                $(this).stop().animate({marginTop: 50});
            }, function() {
                $(this).stop().animate({marginTop: 100});
            });
        })
    </script>

mouseup() 松开鼠标
mousedown() 按下鼠标
mousemove() 鼠标在元素内部移动
keydown() 按下键盘
keypress() 按下键盘
keyup() 松开键盘
load() 元素加载完毕
ready() DOM加载完成
resize() 浏览器窗口的大小发生改变
scroll() 滚动条的位置发生变化
select() 用户选中文本框中的内容
submit() 用户递交表单
toggle() 根据鼠标点击的次数,依次运行多个函数
unload() 用户离开页面

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容