jq知识体系

2017-10-19  本文已影响0人  css7

1 jQuery基础语法

1.1 jquery语法结构

$('选择器').事件名(function(){})

$(document).ready(function(){
    $('.list li').click(function(){
        $(this).css({
            'background':'#fff',
            'color':'red'
        })
    })
})

1.2 设置css属性

$('p').css({
    'color':'#fff',
    'font-size':'18px',
    'background':'blue'
})
$('p').css({
    "color":"#fff",
    "font-size":"18px",
    "background":"blue"
});
$("p").css("color");

1.3 基本选择器

$("p").css({"font-size":"18px","color":"#fff"});
$(".param").css({"font-size":"20px","background":"red"});
$("#only").css({"color":"#fff","background":"#000"});
$("div,p").css({"font-size":"14px"});

1.4 层级选择器

$(".contain p").css({"font-size":"20px","background":"red"});
$(".contain > p").css({"color":"#fff","background":"#000"});

1.5 属性选择器

$('p[name]').css({
    'font-size':'16px',
    'text-align':'center'
})

1.6 过滤选择器


2 jQuery事件

2.1 window事件

2.1.1 jQuery与js加载函数的区别

2.2 鼠标事件

$('#btn').click(function(){
    $('.more').css('display','none');
})
$('#btn2').mouseover(function(){
    $('#box2').show();
});
$('#btn2').mouseout(function(){
    $('#box2').hide();
});

2.3 键盘事件

$(document).keydown(function(event){   //键盘按下事件
    console.log(event.keyCode);
});
$(document).keyup(function(event){  //键盘释放事件
    console.log(event.keyCode);
});

2.4 表单事件

$('input:eq(0)').focus(function() {
    $(this).val('请输入昵称').removeClass('jingshi');
});
$('input:eq(0)').blur(function() {
    $(this).val('改昵称已被注册').addClass('jingshi');
});

2.5 绑定事件

bind(type,[data],fn)

$('.top-m .on').bind('mouseover',function(){
    $('.topDown').css('display','block');
})
$('.top-m .on').bind({
    mouseover:function(){
        $('.topDown').show();
    },
    mouseout:function(){
        $('topDown').hide();
    }
})

2.6 移除事件

unbind([type].fn)

$('li:eq(1)').bind({
    mouseover:function(){
        console.log('你的鼠标在第二个元素上面');
    },
    mouseout:function(){
        console.log('你的鼠标离开了第二个元素');
    }
});
$('li:eq(2)').bind('click',function(){
    $('li:eq(1)').unbind();
});

2.7 hover()方法

模拟鼠标悬停和离开事件
hover(enter,leave)

$('#box3').hover(function(){
    $(this).text('显示文字');
},function(){
    $(this).text('');
});

2.8 toggle()方法

$('#box3').toggle(
    function(){
        $(this).css('background','red');},
    function(){
        $(this).css('background','yellow');},
    function(){
        $(this).css('background','green');}
);
$('#box3').click(function(){
    $('ul').toggle(1000);
})

2.9 isNaN()

用于检查其参数是否是非数字

    console.log(isNaN(123))  //false
    console.log(isNaN('123'))  //false
    console.log(isNaN(5-2))  //false
    console.log(isNaN(2017/12/12))  //false
    console.log(isNaN('hello'))  //true
上一篇 下一篇

猜你喜欢

热点阅读