gpt4 book ai didi

javascript - 元素 style.display 在显示警告框时不会改变

转载 作者:行者123 更新时间:2023-11-27 23:56:34 24 4
gpt4 key购买 nike

下面的 JS 函数应该使元素“shape”在显示警报之前消失,但警报首先显示,“shape”仍然存在。控制台日志表明“shape”style.display 被设置为“none”。如何在显示警报之前让“形状”消失?

<script type="text/Javascript">
var start = new Date().getTime();
document.getElementById("shape").onclick = function() {
document.getElementById("shape").style.display = "none";
console.log(document.getElementById("shape").style.display);
var end = new Date().getTime();
var timeTaken = end - start;
console.log(timeTaken);
alert(timeTaken);
}

</script>

最佳答案

问题是..
这是什么形状?如果是 Canvas ,则有明确的命令,但如果是图像,则有多种方法可以删除源代码并“隐藏”img 标签,如果您愿意的话。

对于像 Divs 和其他 HTML 对象(包括隐藏整个 Canvas )

var example = document.getElementById("shape");
example.style.display = "none";

如果您正在使用 Canvas 进行绘图 (HTML5 Canvas),则删除 Canvas

var can = document.getElementById("Shape");
var surface= can.getContext("2d");
surface.clearRect(0, 0, can.width, can.height);

不过,有点不清楚您要清除的“形状”是什么,或者您希望如何清除它。

关于javascript - 元素 style.display 在显示警告框时不会改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56228371/

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