关闭

jQuery选择器和事件

标签: javascriptprototypejquery对象
180人阅读 评论(0) 收藏 举报
分类:

选择器:

根据元素类型选择

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <script src="jquery-2.1.4.min.js"></script>
    <script src="app.js"></script>
    <!--上下不能写反 依赖关系-->
</head>
<body>
    <p class="pclass"<span style="font-family: Arial, Helvetica, sans-serif;">>p1</p></span>
    <p id="pid">p2</p>
    <button>click me</button>
</body>
</html>

$(document).ready(function(){
    $("button").click(function(){
        $("p").text("p元素被修改了");
    });
});

执行结果 两个p元素都变了

根据元素id选择器:

$(document).ready(function(){
    $("button").click(function(){
        $("#pid").text("p元素被修改了");
    });
});

根据元素class选择:

$(document).ready(function(){
    $("button").click(function(){
        $(".pclass").text("p元素被修改了");
    });
});

事件:

单击、双击、移动消失:
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <script src="jquery-2.1.4.min.js"></script>
    <script src="app.js"></script>
    <!--上下不能写反 依赖关系-->
</head>
<body>
    <button>click me</button>
</body>
</html>
$(document).ready(function(){
   $("button").click(function(){
        $(this).hide();
   }) ;
    $("button").dblclick(function(){
        $(this).hide();
    }) ;
    /*$("button").mouseenter(function(){
        $(this).hide();
    });*/
    $("button").mouseleave(function(){
        $(this).hide();
    });
});

事件绑定、解除绑定:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <script src="jquery-2.1.4.min.js"></script>
    <script src="app.js"></script>
    <!--上下不能写反 依赖关系-->
</head>
<body>
    <button id="click">click me</button>
</body>
</html>

$(document).ready(function(){
    $("#click").bind("click",clickHandler1);
    $("#click").bind("click",clickHandler2);
    $("#click").unbind("click");//所有的都解除绑定
    $("#click").unbind("click",clickHandler2);//只解除绑定2
});
function clickHandler1(e){
    console.log("clickhandler1");
}
function clickHandler2(e){
    console.log("clickhandler2");
}

由于bind unbind实际上是调用了底层的on off所以代码还可以这么写:
$(document).ready(function(){
    $("#click").on("click",clickHandler1);
    $("#click").on("click",clickHandler2);
   // $("#click").off("click");//所有的都解除绑定
    $("#click").off("click",clickHandler2);//只解除绑定2
});
function clickHandler1(e){
    console.log("clickhandler1");
}
function clickHandler2(e){
    console.log("clickhandler2");
}$(document).ready(function(){
    $("#click").on("click",clickHandler1);
    $("#click").on("click",clickHandler2);
   // $("#click").off("click");//所有的都解除绑定
    $("#click").off("click",clickHandler2);//只解除绑定2
});
function clickHandler1(e){
    console.log("clickhandler1");
}
function clickHandler2(e){
    console.log("clickhandler2");
}

事件之事件目标与冒泡

目标:
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <script src="jquery-2.1.4.min.js"></script>
    <script src="app.js"></script>
    <!--上下不能写反 依赖关系-->
</head>
<body>
    <div style="width:300px;height:300px;background-color: chartreuse">
        <ul>
            <li>a</li>
            <li>b</li>
            <li>c</li>
            <li>d</li>
            <li>e</li>
        </ul>
    </div>
</body>
</html>
$(document).ready(function(){
    $("body").bind("click",bodyHander);
    $("div").bind("click",divHander);
});
function bodyHander(event){
    console.log(event);
}
function divHander(event){
    console.log(event);
}
效果:


事件冒泡:
阻止事件冒泡:

event.stopImmediatePropagation()阻止所有事件冒泡

Keeps the rest of the handlers from being executed and prevents the event from bubbling up the DOM tree.

event.stopPropagation()阻止父级事件冒泡

Prevents the event from bubbling up the DOM tree, preventing any parent handlers from being notified of the event.

$(document).ready(function(){
    $("body").bind("click",bodyHander);
    $("div").bind("click",divHander1);
    $("div").bind("click",divHander2);
});
function bodyHander(event){
    conlog(event);
}
function divHander1(event){
    conlog(event);
  //  event.stopPropagation();
    event.stopImmediatePropagation();
}
function divHander2(event){
    conlog(event);
}
function conlog(event){
    console.log(event);
}//防止console编译出错

事件之自定义事件

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <script src="jquery-2.1.4.min.js"></script>
    <script src="app.js"></script>
    <!--上下不能写反 依赖关系-->
</head>
<body>
    <button id="Click">click me</button>
</body>
</html>
$(document).ready(function(){
    $("#Click").click(function(){
        var e=jQuery.Event("myEvent");
        $("#Click").trigger(e);
    });
    $("#Click").bind("myEvent",function(event){
        console.log(event);
    });
});

可以精简为:
var ClickMeBtn;
$(document).ready(function(){
    ClickMeBtn=$("#Click");
    ClickMeBtn.click(function(){
        var e=jQuery.Event("myEvent");
        $("#Click").trigger(e);
    });
    ClickMeBtn.bind("myEvent",function(event){
        console.log(event);
    });
});

也说明了var是多么的强大==










0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    碎碎念
    周小姐,你还想毕业找不到工作吗????
    个人资料
    • 访问:233568次
    • 积分:8244
    • 等级:
    • 排名:第2681名
    • 原创:602篇
    • 转载:25篇
    • 译文:0篇
    • 评论:34条
    友情链接