jQuery 在空白处单击事件

Ada*_*ika 3 jquery

我试图用 jQuery 捕获用户单击页面上的空白区域时的事件。例如,假设您有以下内容:

<html>
<head>
    <title>Test</title>
</head>
    <body>
        <p>Here's some text!</p>
    <body>
</html>
Run Code Online (Sandbox Code Playgroud)

我想在用户单击除p 标签之外的任何内容时捕获该事件。

有没有办法抓住这样的东西?

gre*_*ama 5

这似乎有效:

$(function() {
    $('body').click(function() {
        alert('clicked the page');
        return false;
    });
    $('*:not(body)').click(function() {
        alert('clicked an item!');
        return false;
    });
});
Run Code Online (Sandbox Code Playgroud)

您将遇到的一个“问题”是默认情况下段落是块元素,因此单击句子的一侧将产生项目单击,即使您看起来没有单击项目。