jQuery初学基础常用内容——事件

JQ中的事件方法是很常用的,我这里直接给出我自学时视频中的小案例给看到我博客的朋友参考一下。

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        h1{
            margin:0;
        }
        #card{
            background: #eee;
            padding:20px;
            margin:10px 0;
            display: none;
        }
        #card.active{
            background: #555;
            color:#fff;
        }
    </style>
</head>
<body>

<button id="card-trigger">显示/隐藏公告</button>
<div id="card">
    <h1>公告</h1>
    how are you ? i am fine ,thank you , and you?
</div>

<script src="node_modules/jquery/dist/jquery.js"></script>
<script src="JS/main.js"></script>
</body>
</html>
//js中相关代码
var card = $('#card');
var cardTriggle=$('#card-trigger');

cardTriggle.on('click',function () {
    if(card.is(':visible'))
    {
        card.slideUp();
    }
    else
    {
        card.slideDown();
    }
});
card.on('mouseenter',function () {
    card.addClass('active');
});
card.on('mouseleave',function () {
    card.removeClass('active');
});

猜你喜欢

转载自blog.csdn.net/qq527648162/article/details/84567474