JQ 点击指定文本框显示div。点击其他区域隐藏DIV
<input id="username" type="text" style="width:90%;margin-top: 40px;" placeholder="請輸入車場名稱或車場間稱" class="text_name" name="username"/> <div class="text_div_all" style="display: none;"> <ul> <li>张三3</li> <li>张三1</li> <li>张三2</li> </ul> </div> //点击搜索框显示点击其他区域隐藏 $(document).bind('click',function(e){ var e = e || window.event; //浏览器兼容性 var elem = e.target || e.srcElement; while (elem) { //循环判断至跟节点,防止点击的是div子元素 if (elem.id && elem.id=='username') { return; } elem = elem.parentNode; } $("#username").attr("value",$(".text_div_all ul li").html()); $('.text_div_all').css('display','none'); });