gpt4 book ai didi

javascript - HTML 上的 onselectstart() 函数

转载 作者:行者123 更新时间:2023-11-30 16:13:47 24 4
gpt4 key购买 nike

document.body.onselectstart = function() {
return false;
}

我的 windows.onload = function start() 函数中有这部分代码。当我将鼠标从上到下拖动时,我的上下文没有选择。但是,如果我将鼠标从底部拖到顶部,我可以选择任何我想要的。有没有办法同时防止从上到下和反向拖动?

完整代码如下:

<!DOCTYPE html>
<html>
<head>

</head>
<body oncontextmenu="return false;">

Value:<br> <p id="Value"> 0 </p>

<p>Click the ball to increase the value.</p>

<input type="image" onmousedown="increase()" src="RedBall.png" id="demp" style="position:relative; left: 500px; top: 80px; width: 32px; height: 32px;" />


<script>
function increase() {
var x = document.getElementById("Value").innerHTML;
var increased = parseInt(x,10) + parseInt(1,10);
document.getElementById("Value").innerHTML = increased;
}

function change(){
var num = 0;
window.setInterval(function(){
var speedx= Math.floor((Math.random() * 11) - 5);
var speedy= Math.floor((Math.random() * 11) - 5);
document.getElementById("demp").style.left= parseInt(document.getElementById("demp").style.left, 10) + parseInt(speedx,10) +"px";
document.getElementById("demp").style.top= parseInt(document.getElementById("demp").style.top, 10) + parseInt(speedy,10) +"px";
}, 10)
}
window.onload = function start() {
change();
document.body.onselectstart = function() {
return false;
}
}
</script>
</body>
</html>

https://output.jsbin.com/niyijuqaco

你也可以在这个网站上看到结果

最佳答案

问题

body 标签不能代替所有窗口:

enter image description here

解决方案那么,解决方案很简单。您需要将 html 的高度(只是为了忍受)和 body 设置为 100%

enter image description here

现在,body 获取窗口的大小和触发事件 onselectstart

<!DOCTYPE html>
<html>
<head>
<style>
html, body {
height:100%;
}
</style>
</head>
<body oncontextmenu="return false;">

Value:<br> <p id="Value"> 0 </p>

<p>Click the ball to increase the value.</p>

<input type="image" onmousedown="increase()" src="RedBall.png" id="demp" style="position:relative; left: 500px; top: 80px; width: 32px; height: 32px;" />


<script>
function increase() {
var x = document.getElementById("Value").innerHTML;
var increased = parseInt(x,10) + parseInt(1,10);
document.getElementById("Value").innerHTML = increased;
}

function change(){
var num = 0;
window.setInterval(function(){
var speedx= Math.floor((Math.random() * 11) - 5);
var speedy= Math.floor((Math.random() * 11) - 5);
document.getElementById("demp").style.left= parseInt(document.getElementById("demp").style.left, 10) + parseInt(speedx,10) +"px";
document.getElementById("demp").style.top= parseInt(document.getElementById("demp").style.top, 10) + parseInt(speedy,10) +"px";
}, 10)
}
window.onload = function start() {
change();
document.body.onselectstart = function() {
return false;
}
}
</script>
</body>
</html>

关于javascript - HTML 上的 onselectstart() 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35825676/

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