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

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

问题描述

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

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

JavaScript c.js:

JavaScript c.js:

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

HTML:

<!DOCTYPE html>
<html>

<head>
  <script src="c.js"></script>
</head>

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

</html>

我可以从控制台运行该功能,但单击 <body> 不起作用.

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

推荐答案

<body> 元素没有高度.添加如下代码:

The <body> element has no height. Add something like the code below:

<!DOCTYPE html>
<html>

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

<body onclick="clickBody();/>  

</html>

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

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