jQuery: Hide element on click anywhere else apart of the element
本问题已经有最佳答案,请猛点这里访问。
Possible Duplicate:
How to detect a click outside an element?
我试图实现如果用户单击元素以外的任何地方,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | $('.nav-toggle').click(function() { //get collapse content selector var collapse_content_selector = $(this).attr('href'); //make the collapse content to be shown or hide var toggle_switch = $(this); $(collapse_content_selector).toggle(function() { if ($(this).css('display') == 'none') { //change the button label to be 'Show' toggle_switch.html('Show Details'); } else { //change the button label to be 'Hide' toggle_switch.html('Hide Details'); } }); }); |
您可以采用事件委托的概念。
1 2 3 4 5 6 7 8 9 10 | $(function() { $(document).on('click', function(e) { if (e.target.id === 'div1') { alert('Div Clicked !!'); } else { $('#div1').hide(); } }) });? |
检查小提琴
我不明白你和另一部分结合是什么意思。这是基本的想法。
当用户单击其他元素(如link、botton、what thever…)时,可以使用
当一个元素失去焦点时,模糊事件将被激发(用户在DOM树上选择另一个元素)
我不明白你拨动开关的链接。如果切换按钮管理DIV,则应在更新按钮文本的同时将hide()/show()放在该DIV的切换函数内。