美文网首页
绑定click事件

绑定click事件

作者: 暴走的金坤酸奶味 | 来源:发表于2018-09-18 23:17 被阅读0次

给元素绑定click事件,可以用如下方法

$('#btn1').click(function(){

    // 内部的this指的是原生对象

    // 使用jquery对象用 $(this)

})

例子


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>click事件</title>
    <style type="text/css">
        .box{
            width: 200px;
            height: 200px;
            background-color: gold;
        }
        .sty{
            background-color: green;
        }
    </style>
    <script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
    <script type="text/javascript">
        $(function(){
            // 给按钮绑定click事件
            $('#btn').click(function(){
                //重复切换sty样式
                $('.box').toggleClass('sty');
            })
        })
    </script>
</head>
<body>
    <input type="button" value="切换" id="btn">
    <div class="box"></div>
</body>
</html>

相关文章

网友评论

      本文标题:绑定click事件

      本文链接:https://www.haomeiwen.com/subject/fqtbnftx.html