JQuery 的小技巧

来源:互联网 发布:免费网络英语课堂 编辑:程序博客网 时间:2024/05/17 23:45
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        #d1{width:100px;height:20px;background: paleturquoise}
        #d2{width:40px;height:20px;background: palegoldenrod}
        #d3{width:80px;height:20px;background: pink}
    </style>
</head>
<body>
    <div id="xy">XY</div>
    <div id="d1"></div>
    <div id="d2"></div>
    <div id="d3"></div>
    <div id="d4">div4</div>
    <a href="http://www.baidu.com">aaa</a>
</body>
<script src="js/jquery-1.11.3.min.js"></script>
<script>
    //1.鼠标右键禁用
//    $(document).on("contextmenu",function(){
//        return false
//    });
    //2.鼠标键值
    $("#xy").mousedown(function(e){
        alert(e.which);//1,2,3
        //左键1,
        //中间2.
        //右键3.
    });
    //3.自定义选择器
    var select="widthMroe50px";
    $.expr[":"][select]=function(a){
        return $(a).width()>50
    };
    console.log(select);
    $("div:widthMroe50px").click(function(){
        alert("我大于50px");
    });
    //4.跳转链接
    $("#d4").click(function(){
        window.location=$(this).next("a").attr("href");


    });
    //5.改变this;
    //call/apply
    var n=10;
    function fn(){
        console.log(this.n)
    }
    var obj={
        n:"n",
        fn:fn
    };
    obj.fn();//"n"
    fn();//10
    fn.call(obj)//"n"
//    fn();//10->"n"
</script>
</html>
0 0
原创粉丝点击