gpt4 book ai didi

javascript - event.preventDefault() 不会阻止代码运行

转载 作者:行者123 更新时间:2023-11-28 15:03:05 25 4
gpt4 key购买 nike

我有以下 html 页面和以下 javascript :

该页面应该在随机位置显示图片并每 4 秒刷新一次(屏幕保护程序页面)。如果未检测到鼠标操作或平板电脑触摸(因此在设置页面中添加了事件监听器),则该操作将永远继续。它一直工作得很好,直到我修改了 goActive 函数以添加 window.open("index.html") 行。现在的问题是屏幕保护程序永远不会显示,它立即按照下一条指令打开index.html页面。我认为添加行 event.preventDefault 可以解决我的问题。

知道我现阶段做错了什么吗?

function init() {
var xmin = 0;
var xmax = 890;
var ymin = 0;
var ymax = 430;

var xCoord = Math.floor((Math.random()*xmax)+xmin);
var yCoord = Math.floor((Math.random()*ymax)+ymin);

var xCoordStr = xCoord.toString() + "px";
var yCoordStr = yCoord.toString() + "px";

document.getElementById("randomPlacement").style.left = xCoordStr;
document.getElementById("randomPlacement").style.top = yCoordStr;

document.getElementById("date").innerhtml=getDate('date');
document.getElementById("time").innerhtml=getTime('time');
}
function setup() {
this.addEventListener("mousemove", exitScreenSaver, false);
this.addEventListener("mousedown", exitScreenSaver, false);
this.addEventListener("keypress", exitScreenSaver, false);
this.addEventListener("DOMMouseScroll", exitScreenSaver, false);
this.addEventListener("mousewheel", exitScreenSaver, false);
this.addEventListener("touchmove", exitScreenSaver, false);
this.addEventListener("MSPointerMove", exitScreenSaver, false);
}

function exitScreenSaver(e) {
goActive();
}

function goActive() {
// do something
console.log(".. active ..");
event.preventDefault();
window.open("index.html","_self");
}
 /* Page entière (utilisé pour avoir une couverture globale en couleur) */
body {
background-color:black; /*screensaver mode*/
}

/* Affichage de date (en haut) */
#date{
font-family:Arial;
font-size: 130%;
font-weight: 800;
text-align: left;
position: fixed;
top: 5px;
left: 20px;
color:white; /* screensaver mode*/
}

/* Affichage d'heure (en haut) */
#time{
font-family:Arial;
font-size: 130%;
font-weight: 800;
text-align: left;
position: fixed;
top: 5px;
left: 300px;
color:white; /* screensaver mode*/
}
/* Affichage de la température */
#tag_temperature{
font-family:Arial;
font-size: 130%;
font-weight: 800;
text-align: left;
position: fixed;
top: 5px;
left: 600px;
color:black; /* night mode*/
color:blue; /* day mode*/
color:white; /* screensaver mode*/
}

p {margin: 0;}
<!DOCTYPE html>
<!--
Page d'économiseur d'écran, créée le 30.07.2016
modifiée le 02.09.2016
modifiée le 22.10.2016 (no cache)
modifiée le 23.10.2016 (inclusion de la date et de l'heure)
-->
<html>
<head>
<title>Domoos | Screen saver screen</title>
<meta http-equiv="refresh" content="4">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0"/>
<meta http-equiv="pragma" content="no-cache">
<link rel="stylesheet" type="text/css" href="css/mystyle_saver.css" />
<script type="text/javascript" src="scripts/date_time.js"></script>
<script type="text/javascript" src="scripts/screensaver.js"></script>
</head>
<body onload="init(); setup();">
<div style="position:absolute" id="randomPlacement">
<p><a href="index.html"><img src="assets/pictures/texte_sortie_veille.png" alt ="" style="width:60px;height:60px;"></a></p>
</div>
<div id="date"></div>
<div id="time"></div>
<div id="tag_temperature">
<p>?&deg;C</p>
</div>
</body>
</html>

最佳答案

只需将 e 参数传递给 goActive 函数即可

function exitScreenSaver(e) {
goActive(e);
}

function goActive(event) {
// do something
console.log(".. active ..");
event.preventDefault();
window.open("index.html","_self");
}

关于javascript - event.preventDefault() 不会阻止代码运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40226084/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com