我在这里提问是因为找不到答案。目前我没有任何触摸设备,所以无法进行测试。
以下代码将隐藏容器外点击的所有子容器。
$(document).mouseup(function(e) {
var container = $('#container');
if (!container.is(e.target) // if the target of the click isn't the container...
&& container.has(e.target).length === 0) // ... nor a descendant of the container
{
$('.subcontainer').hide();
}
});
这个功能在触摸设备上能用吗?或者有没有相当于 mouseup
的触摸设备事件?
#container
是body
的子元素,所以将bind
附加到body
会更快 :) - Rafff