为什么toggle()能处理文档而不能处理正文

why does toggle() work with document but not body

本文关键字:处理 文档 不能 正文 toggle 为什么      更新时间:2023-09-26

我在处理一些jQuery时发现toggle()只在从文档而不是从正文中触发时才切换。

例如

$(document).click(function(){
    $('div').toggle();
});
//this toggles
$('body').click(function(){
    $('div').toggle();
});
//this doesn't toggle

为什么不与body切换?

FIDDLE HERE

这是因为当你的div不在时,bodyheight就是0,所以当div不在时你不能点击它。

只要给一些height&width到您的body

body{
    height:1000px;
    width:1000px;
}

DEMO