gpt4 book ai didi

javascript - 解释为什么这个 HTML 只能在 Firefox 中使用,而不能在任何其他浏览器中使用?

转载 作者:行者123 更新时间:2023-11-28 04:45:37 25 4
gpt4 key购买 nike

大家。我大约一周大的 HTML/CSS/Javascript 初学者。我为一个页面编写了这段代码,该页面旨在每当鼠标在页面上移动时根据“随机”RGB 值更改颜色,alpha 值始终固定为 1。

<!DOCTYPE html>
<head>
<script src='https://code.jquery.com/jquery-1.10.2.js'></script>
</head>
<body>
<script>
$('*').mousemove(function() {
var red = Math.floor(Math.random() * 255);
var green = Math.floor(Math.random() * 255);
var blue = Math.floor(Math.random() * 255);

var rgba = 'rgba(' + red + ',' + blue + ',' + green + ')';
$('*').css('background', rgba);
});
</script>
</body>

我尝试过 Chrome、Chromium、Vivaldi、Opera 甚至 Internet Explorer,所有这些浏览器都显示了一个网页,其中默认的白色背景页面对鼠标移动没有反应。

最佳答案

您的代码的主要问题是您正在设置 RGBA 颜色,但您忘记提供 A 值。

我还建议您使用 document 来监听 mousemove 事件并更新 body 的 background-color 。应尽可能避免使用通配符 * 选择器,因为它很慢 - 特别是在附加事件处理程序时。

$(document).mousemove(function() {
var red = Math.floor(Math.random() * 255);
var green = Math.floor(Math.random() * 255);
var blue = Math.floor(Math.random() * 255);

var rgba = 'rgba(' + red + ',' + blue + ',' + green + ',1)'; // note '1' at the end
$('body').css('background', rgba);
});
<script src='https://code.jquery.com/jquery-1.10.2.js'></script>

这个片段应该有一个癫痫警告......

关于javascript - 解释为什么这个 HTML 只能在 Firefox 中使用,而不能在任何其他浏览器中使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47416119/

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