为什么body元素上的onclick事件不起作用? [英] Why is the onclick event on the body element not working?

查看:154
本文介绍了为什么body元素上的onclick事件不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



JavaScript c.js

我试图在点击html body时将用户发送到另一个页面。 :

 函数clickBody(){
window.location.href ='/';
}

HTML:

 <!DOCTYPE html> 
< html>
< head>
< script src =c.js>< / script>
< / head>

< body onclick =clickBody();>
< / body>
< / html>

我可以从控制台运行该函数,但单击< body>

解决方案

< body>

 <!DOCTYPE html>元素没有高度,请添加如下所示的元素: 
< html>
< head>
< style>
body {
height:500px;
}
< / style>
< script src =c.js>< / script>
< / head>

< body onclick =clickBody();>

< / body>
< / html>


I'm trying to send users to another page when click html body:

JavaScript c.js:

function clickBody(){
    window.location.href = '/';
}

HTML:

<!DOCTYPE html>
<html>
<head>
    <script src="c.js"></script>
</head>

<body onclick="clickBody();">
</body>  
</html>

I can run the function from console, but clicking the <body> is not working.

解决方案

<body> element have no height, add something like bellow:

<!DOCTYPE html>
<html>
<head>
  <style>
    body {
     height: 500px;
    }
  </style>
  <script src="c.js"></script>
</head>

<body onclick="clickBody();">

</body>  
</html>

这篇关于为什么body元素上的onclick事件不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆