简体   繁体   English

单击页面上其他任何位置时隐藏列表项

[英]Hide list items when clicked anywhere else on the page

I have created a search user functionality but having an issue whenever user clicks anywhere else on the page the list does not disappear until unless the user deletes all the words which he/she have written to show the matching result which is not a good thing I tried a lot but still it is not working the div is visible no mater where ever we click on all around the page. 我已经创建了搜索用户功能,但是每当用户单击页面上的其他任何地方时,列表都会消失,直到用户删除他/她写的所有单词以显示匹配结果的问题,这都是一件不好的事情,尝试了很多,但仍然无法正常工作,我们在页面上单击的所有位置都看不到div。

 $(document).ready(function(e) { var start = /@/ig; var word = /@(\\w+)/ig; var $contentbox = $('#contentbox'), $display = $('#display'); $contentbox.on('keyup', function(e) { var target = e.target; var $box = $(this), content = $box.text(), go = content.match(start), name = content.match(word); len = $.trim($contentbox.text()).length; if (!$(target).is('#display') && !$(target).parents().is('#display')) { $('#display').hide(); } if (go && go.length) { $display.slideUp('show'); if (name && name.length) { var html = '<ul class="tg_1"><li><p>test 1</p></li></ul><ul class="tg_1"><li><p>test 1</p></li></ul>'; $display1.html(html).show(); } console.log(go.length); } return false; }); $(document).on("click", ".addname", function() { var username = $(this).attr('title'); var old = $("#contentbox").html(); var content = old.replace(word, ""); $("#contentbox").html(content); var E = username; $("#contentbox").append(E); $("#display").hide(); $("#contentbox").focus(); }); }); 
 .st_n_ar>form { position: relative; } #display_co, #display { position: absolute; width: 98%; background-color: #fff; padding: 10px; left: 6px; top: 123px; display: none; z-index: 99; } .st_n_ar>form>#contentbox { width: 98%; height: 81px; margin: auto; background-color: #efefef; padding: 15px 12px; padding-bottom: 45px; overflow: auto; margin-bottom: 40px !important; } .st_n_ar>form>#sb_art { position: absolute; right: 6px; bottom: -35px; background-color: #0d1927; border: none; height: 35px; line-height: 35px; text-transform: uppercase; font-size: 12px; color: #fff; padding: 0 15px; transition: ease-in-out all .5s; -webkit-transition: ease-in-out all .5s; -moz-transiotion: ease-in-out all .5s; cursor: pointer; } ul.tg_1 { padding-left: 0; list-style-type: none; overflow: hidden; margin: 0; } .tg_1>li { margin: 5px auto; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="st_n_ar"> <form method="POST" action="" id="postform" enctype="multipart/form-data" onsubmit="return false;"> <div id='display'></div> <div id='display_co'></div> <div id="contentbox" contenteditable="true" name="post_dt" max="200"> </div> <input type="submit" id="sb_art" class="btn_v2" value="Start Discussion" /> </form> </div> 

Jquery Fiddle Demo jQuery小提琴演示

are you looking for this? 你在找这个吗?

$(document).mouseup(function(e) {
    var container = $("#contentbox");

    // if the target of the click isn't the container nor a descendant of the container
    if (!container.is(e.target) && container.has(e.target).length === 0) {
        $('#display').html('').hide(); // hide the result div block
        $("#contentbox").text(''); // clear the field
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在页面加载时打开一个弹出窗口并在点击其他任何地方时隐藏 + 在调整窗口大小时动态跟随它的父级 - Open one popover on page load and hide when clicked anywhere else + dynamically follow it's parent when resizing window 单击页面上的其他任何位置时,3 个可单击的下拉菜单不会关闭 - 3 clickable dropdowns do not close when clicked anywhere else on the page 单击页面上的任意位置时隐藏元素,并通过单击其他元素来显示它 - hide element when clicked anywhere on the page and show it by click on other element 单击页面上任何位置时如何隐藏下拉菜单 - How to hide dropdown menu when clicked anywhere on the page 当用户单击链接或页面中其他任何位置时,JS隐藏菜单 - JS to hide menu when users click on a link or anywhere else in the page 单击任何地方但菜单时,jQuery隐藏下拉列表 - jQuery hide dropdown when clicked anywhere but menu 单击文档上任何位置时,隐藏弹出框 - Hide popup box when clicked anywhere on the document 如果在可见表单旁边单击页面上的其他任何位置,则关闭搜索表单 - Close search form if anywhere else on page is clicked besides the visible form 单击页面上任何位置时关闭div - Close a div when clicked anywhere on page 点击页面上的任何位置时,jQuery会向后滑动 - jQuery slide back when clicked anywhere on the page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM