«

JQ 点击其它地区, 指定区域隐藏

时间:2023-3-2 09:57     作者:wen     分类: F2E


<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .btn {
            width: 200px;
        }

        .div {
            width: 200px;
            height: 200px;
            background-color: blue;
            display: none;
        }
    </style>
</head>

<body>
    <form action="" enctype="application/x-www-form-urlencoded"></form>
    <input type="text" class="search-in">
    <div class="div" id="associate">

    </div>
    <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
    <script>

        $(".search-in").focus(function(){
            console.log(123);
        })

        $('body').bind('click', function (e) {
            e = e || window.event;
            let elem = e.target || e.srcElement;
            // JS object to JQ object
            let ele = $(elem);
            console.log(ele)
            if(ele && ele.is('.search-in')){
                $('#associate').css("display",'flex');
                return;
            }
            while (ele) {
                if (ele && ele.attr('id') == 'associate') {
                    return;
                }
                if(ele.length == 0){
                    $('#associate').css("display",'none');
                    return;
                }else{
                    ele = ele.parent();
                }
            }
        });

    </script>
</body>

</html>

标签: jquery