gpt4 book ai didi

javascript - jQuery 出现一些错误,无法隐藏元素

转载 作者:行者123 更新时间:2023-12-01 08:29:40 25 4
gpt4 key购买 nike

一个jQuery问题

我想用一个函数让一个元素立即隐藏。这是我的:

<!DOCTYPE html>
<html>
<head>
<title>A test</title>
</head>
<body>
<script src="https://code.jquery.com/jquery-2.1.0.js"></script>
<h1 id="heading">This will hide instantly after 3 seconds.</h1>
<script>
var hide = function (time) {
$("#heading").fadeOut(time);
};
setTimeout(hide(0), 3000);
</script>
</body>
<html>

但是,标题不会在开始时显示。如果这对您不起作用,请尝试等待 4 秒并重新加载页面。

注意:我使用的是 TextEdit,浏览器是 Google Chrome。

但是,这有效:

<!DOCTYPE html>
<html>
<head>
<title>A test</title>
</head>
<body>
<script src="https://code.jquery.com/jquery-2.1.0.js"></script>
<h1 id="heading">This will hide instantly after 3 seconds.</h1>
<script>
var hide = function () {
$("#heading").fadeOut(0);
};
setTimeout(hide, 3000);
</script>
</body>
<html>

我只想创建一个可以容纳所有数字的函数,因此我只需要创建函数而不是数百万。

如何稍微更改代码以使第一个代码正常工作?

最佳答案

您需要将函数参数传递到第三个参数,因此请使用此 setTimeout(hide, 3000, 0); 更改设置超时代码

<!DOCTYPE html>
<html>
<head>
<title>A test</title>
</head>
<body>
<script src="https://code.jquery.com/jquery-2.1.0.js"></script>
<h1 id="heading">This will hide instantly after 3 seconds.</h1>
<script>
var hide = function () {
$("#heading").fadeOut(arguments[0]);
};
setTimeout(hide, 3000, 0);
</script>
</body>
<html>

关于javascript - jQuery 出现一些错误,无法隐藏元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61883793/

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