Jquery - 背景图像点击

Jen*_*nan 1 javascript jquery click background-image

我在页面的中心有一个div.我身体里有背景图像.我只需要在background-image上使用jquery click.如果单击id为divPage的div,则没有任何反应.

感谢您的建议或可能的其他解决方案.

HTML:

<body>
    <form id="formBackground" method="post">

        <div id="divPage">
    Text.
        </div>
    </form>
</body>
Run Code Online (Sandbox Code Playgroud)

CSS

body{
padding: 0px;
margin: 0px;
font-family: Arial, Verdana, Helvetica, sans-serif;
font-size: 12px;
color: white;
background-image: url('../images/backgrounds/bg.jpg');
background-repeat:no-repeat;
background-position:top;}

div#divPage{
width: 800px;
height: 300px;
margin: auto;}
Run Code Online (Sandbox Code Playgroud)

我尝试过这个,但它无法正常工作:

$('body').click(function() {
            alert("Click");
        })
Run Code Online (Sandbox Code Playgroud)

它是用id id divPage点击div.

Yos*_*shi 8

你可以试试:

$(function () {
  $('body').bind('click', function (evt) {
    if(evt.target == $('body')[0]) {
      console.log('body clicked');
    }
  });
});
Run Code Online (Sandbox Code Playgroud)

请注意,如果您的内容的高度非常低,您可能需要以下内容:

body, html {
  height: 100%;
}
Run Code Online (Sandbox Code Playgroud)

在你的CSS中,或者没有可点击的身体.

演示:http://jsfiddle.net/mp4TH/