怎么阻止冒泡啊

怎么阻止冒泡啊

<!DOCTYPE html>

<html lang="en">


<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <meta http-equiv="X-UA-Compatible" content="ie=edge">

    <link rel="stylesheet" href="./css/common.css">

    <link rel="stylesheet" href="./css/index.css">

    <link rel="stylesheet" href="./css/ui.css">

    <title>幕淘网</title>

</head>


<body>

    <!-- top部分 -->

    <div class="top">

        <div class="center">

            <div class="left top-left">

                <ul>

                    <li class="left">

                        <a href="#1" style="color: #f01414;">亲,请登录</a></li>

                    <li class="left"><a href="#1">免费注册</a></li>

                    <li class="left"><a href="#1">手机逛幕多多</a></li>

                </ul>

            </div>

            <div class="right">

                <div class="top-dropdown">

                    <div class="left dropdown-item  ">

                        <div class="dropdown-toggle">

                            <a href="">我的幕多多</a>

                            <i class="dropdown-icon"></i>

                        </div>


                        <ul class="dropdown-layer dropdown-left">

                            <li>

                                <a href="" class="dropdown-items">已买到的宝贝</a>

                                <a href="" class="dropdown-items">我的足迹</a>

                            </li>

                        </ul>

                    </div>

                    <div class="left dropdown-item">

                        <div class="dropdown-toggle"> <a href="">  收藏夹</a><i class="dropdown-icon"></i></div>

                        <ul class="dropdown-layer dropdown-left">

                            <li><a href="###" target="_blank" class="dropdown-items">收藏的宝贝</a></li>

                            <li><a href="###" target="_blank" class="dropdown-items">收藏的店铺</a></li>

                        </ul>

                    </div>

                    <div class="left dropdown-item">

                        <div class="dropdown-toggle"> <a href="">  商品分类</a></div>


                    </div>

                    <div class="left dropdown-item">

                        <div class="dropdown-toggle"><a href="">卖家中心</a><i class="dropdown-icon"></i></div>

                        <ul class="dropdown-layer dropdown-left">

                            <li><a href="###" target="_blank" class="dropdown-items">免费开店</a></li>

                            <li><a href="###" target="_blank" class="dropdown-items">已卖出的宝贝</a></li>

                            <li><a href="###" target="_blank" class="dropdown-items">出售中的宝贝</a></li>

                            <li><a href="###" target="_blank" class="dropdown-items">卖家服务市场</a></li>

                            <li><a href="###" target="_blank" class="dropdown-items">卖家培训中心</a></li>

                            <li><a href="###" target="_blank" class="dropdown-items">体验中心</a></li>

                        </ul>

                    </div>

                    <div class="left dropdown-item ">

                        <div class="dropdown-toggle"><a href=""> 联系客服</a><i class="dropdown-icon"></i></div>

                        <ul class="dropdown-layer dropdown-right">

                            <li><a href="###" target="_blank" class="dropdown-items">消费者客服</a></li>

                            <li><a href="###" target="_blank" class="dropdown-items">卖家客服</a></li>

                        </ul>

                    </div>

                </div>

            </div>

        </div>

    </div>


    <!----------------------------------------    top部分  ------------------------------->



    <!---------------------------------- logo部分------------------------------- -->

    <div class="logo">

        <div class="center">

            <div class="logo-logo left">

                <a href="" class="logo-logo-img"></a>

            </div>

            <div class="logo-search left">

                <form action="https://s.taobao.com/search" id="form">

                    <input type="text" placeholder="灵魂没事一元抢" name="q" class="left search-input" autocomplete="off"></input>

                    <input type="submit" value="搜索" class="left search-submit"></input>

                </form>

                <ul class="search-layer">

                    <!--  <li class="search-layer-item text-ellipsis">123</li>

                    <li class="search-layer-item text-ellipsis">123</li>

                    <li class="search-layer-item text-ellipsis">123</li> -->

                </ul>

            </div>


        </div>

    </div>

    </div>

    </div>

    <!-- -----------------------------------logo部分----------------------------------------- -->


    <script type="text/javascript" src="./js/jq.js"></script>

    <script type="text/javascript" src="./js/top.js"></script>

    <script type="text/javascript" src="./js/search.js"></script>


</body>


</html>

.top {

    height: 44px;

    line-height: 44px;

    background: #f3f5f7;

    font-size: 10px;

}


.top .center {

    border-bottom: 1px solid #cdd0d4;

    height: 100%;

}


.top .top-left li a {

    display: inline-block;

    padding-left: 25px;

    color: #4d555d;

}


.top-dropdown .dropdown-item a:hover,

.top .top-left li a:hover {

    color: #f01414;

    text-decoration: none;

}


.top-dropdown {

    height: 100%;

}


.dropdown-toggle {

    position: relative;

    z-index: 2;

    display: block;

    height: 100%;

    padding: 0 16px 0 12px;

    border-left: 1px solid #f3f5f7;

    border-right: 1px solid #f3f5f7;

}


.top-dropdown .dropdown-item {

    position: relative;

}


.dropdown-icon {

    display: inline-block;

    width: 8px;

    height: 6px;

    background: url(../作业素材/code/img/dropdown-arrow.png) no-repeat;

    margin-left: 8px;

    vertical-align: middle;

    cursor: pointer;

}


.top-dropdown .dropdown-item a {

    color: #4d555d;

}


.dropdown-left {

    left: 0;

    right: auto;

}


.dropdown-right {

    right: 0;

    left: auto;

}


.dropdown-layer {

    display: none;

    position: absolute;

    top: 43px;

    background-color: #fff;

    z-index: 1;

    border: 1px solid #cdd0d4;

}


.dropdown-items {

    display: block;

    height: 30px;

    line-height: 30px;

    padding: 0 12px;

    color: #4d555d;

    white-space: nowrap;

}


.dropdown-items:hover {

    background-color: #f3f5f7;

}


.dropdown-active .dropdown-toggle,

.dropdown:hover .dropdown-toggle {

    background-color: #fff;

    border-color: #cdd0d4;

}


.dropdown-active .dropdown-icon,

.dropdown:hover .dropdown-icon {

    background-image: url(../作业素材/code/img/dropdown-arrow-active.png);

}


.dropdown-active .dropdown-layer,

.dropdown:hover .dropdown-layer {

    display: block;

}



/* -----------------top部分------------------------ */



/* ------------------logo部分------------------- */


.logo {

    background: #f3f5f7;

    line-height: 126px;

    height: 126px;

    width: 100%;

}


.logo-logo a {

    cursor: pointer;

    display: block;

    width: 212px;

    height: 58px;

    margin: 36px 0 0 15px;

    text-indent: -9999px;

    overflow: hidden;

    background: url(../作业素材/code/img/header-logo.png) no-repeat;

}


.logo-search {

    width: 680px;

    height: 42px;

    border: 1px solid #cfd2d5;

    margin-top: 38px;

    margin-left: 150px;

    position: relative;

}


.search-input {

    width: 586px;

    height: 42px;

    border: none;

    background: #fff;

    padding: 0 10px;

    line-height: 42px;

}


.search-submit {

    border: none;

    outline: none;

    background: #ca003f;

    color: #fff;

    height: 42px;

    width: 74px;

    cursor: pointer;

}


.search-layer {

    display: none;

    position: absolute;

    top: 100%;

    left: -1px;

    width: 100%;

    background-color: #fff;

    border: 1px solid #cfd2d5;

}


.search-layer-item {

    height: 24px;

    line-height: 24px;

    padding: 0 10px;

    cursor: pointer;

}


.search-layer-item:hover {

    background-color: #f3f5f7;

}



/* ------------------logo部分------------------- */

.clearfloat {

    zoom: 1;

}


.clearfloat:after {

    display: block;

    clear: both;

    content: "";

    visibility: hidden;

    height: 0;

}


* {

    margin: 0;

    padding: 0;

}


body {

    overflow-y: scroll;

}


a {

    text-decoration: none;

    cursor: pointer;

}


a:hover {

    text-decoration: none;

}


ul,

li {

    list-style: none;

}


.right {

    float: right;

    display: inline;

}


.left {

    display: inline;

    float: left;

}


.center {

    margin: 0 auto;

    width: 1200px;

}


.text-ellipsis {

    text-overflow: ellipsis;

    white-space: nowrap;

    overflow: hidden;

}

(function($) {

    var $search = $(".logo-search");

    var $form = $search.find("#form");

    var $input = $search.find(".search-input");

    var $submit = $search.find(".search-submit");

    var $layer = $search.find(".search-layer");

    var $layerItem = $search.find(".search-layer-item");



    $form.on('submit', function() {

        if ($.trim($input.val()) === '') {

            return false;

        }

    });


    $input.on("input", function() {

        var url = 'https://suggest.taobao.com/sug?code=utf-8&_ksTS=1484204931352_18291&callback=jsonp18292&k=1&area=c2c&bucketid=6&q=' +

            encodeURIComponent($.trim($input.val()));

        //encodeURIComponent 把字符串作为URI组件进行编码


        $.ajax({

            url: url,

            dataType: 'jsonp'

        }).done(function(data) {

            var html = "",

                dataNum = data['result'].length,

                maxNum = 10;

            if (dataNum === 0) {

                $layer.hide().html('');

                return

            }

            for (var i = 0; i < dataNum; i++) {

                if (i >= maxNum) break;

                html += '<li class="search-layer-item text-ellipsis">' + data['result'][i][0] + '</li>';

            }

            $layer.show().html(html);

        }).fail(function() {

            $layer.hide().html('');

        }).always(function() {

            console.log('why always me!');

        });

    });


    $layer.on('click', '.search-layer-item', function() {

        $input.val($(this).text());

        $layer.hide();

    })


    $input.on("focus", function() {

            $layer.show();

        })

        /* .on("blur", function() {

                    $layer.hide();

                }) */

        /*  $(document).on('click', function() {

             $layer.hide();

         }) */




})(jQuery);



怎么才能点击外面的时候下拉列表隐藏

正在回答

登陆购买课程后可参与讨论,去登陆

3回答

同学你好,老师这里测试,这样写,再次点击输入框的时候,还是可以显示下拉菜单的。

祝学习愉快~

  • 繁湖 提问者 #1
    检查出问题了,方法写错了,谢谢老师
    2019-12-17 10:22:09
好帮手慕糖 2019-12-16 16:06:43

同学你好,如下,可以给输入框绑定事件点击事件的时候,阻止冒泡,例:

http://img1.sycdn.imooc.com//climg/5df73af1098de55d04780521.jpg

如果我的回答帮助了你,欢迎采纳,祝学习愉快~

  • 提问者 繁湖 #1
    但是这样之后再次点击输入框,下拉列表根本不显示了额,我就是再次点击不显示这个问题,怎么可以保持点击输入框就可以显示
    2019-12-16 16:40:58
好帮手慕糖 2019-12-16 15:30:03

同学你好,阻止冒泡可以使用 event.stopPropagation();

不同,同学说的是那一部分的下拉菜单?这里没有下拉菜单的显示,同学可以查看下是否是代码没有粘贴完整,可以详细的描述下。

祝学习愉快~

  • 提问者 繁湖 #1
    是点击搜索的下拉列表,从淘宝获取的那个,,,就是想点击空白处那个下拉列表隐藏,但是好像加了 $(document).on('click', function() { $layer.hide(); })后,点击输入框也会隐藏下拉列表
    2019-12-16 15:55:51
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
2.组件化网页开发
  • 参与学习           人
  • 提交作业       1121    份
  • 解答问题       14456    个

本阶段在运用JS实现动态网页开发的基础上,带你深入理解企业开发核心思想,完成一个企业级网页的开发,体验前端工程师的成就感。

了解课程
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师