如何更改onclick成为onload?

时间:2015-03-11 07:45:59

标签: javascript fullscreen html5-fullscreen

我希望自动使页面全屏,而无需点击任何内容。我试图改变这一点 <body onclick="toggleFullScreen()"> <body onload="toggleFullScreen()">function errorHandler() { alert('mozfullscreenerror'); } document.documentElement.addEventListener('mozfullscreenerror', errorHandler, false); // toggle full screen function toggleFullScreen() { if (!document.fullscreenElement && // alternative standard method !document.mozFullScreenElement && !document.webkitFullscreenElement) { // current working methods if (document.documentElement.requestFullscreen) { document.documentElement.requestFullscreen(); } else if (document.documentElement.mozRequestFullScreen) { document.documentElement.mozRequestFullScreen(); } else if (document.documentElement.webkitRequestFullscreen) { document.documentElement.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT); } } else { if (document.cancelFullScreen) { document.cancelFullScreen(); } else if (document.mozCancelFullScreen) { document.mozCancelFullScreen(); } else if (document.webkitCancelFullScreen) { document.webkitCancelFullScreen(); } } }。但没有任何反应;它不起作用。 :(

这是javascript ..

{{1}}

1 个答案:

答案 0 :(得分:1)

在任何浏览器中,如果没有用户手势,无法执行任何RequestFullScreen。

顺便说一下,你的全屏功能可以是这样的:

  function fullScreen() {
    var el = document.documentElement;
    var rfs = el.requestFullScreen
              || el.webkitRequestFullScreen
              || el.mozRequestFullScreen;
    rfs.call(el);
  }