gpt4 book ai didi

javascript - 除非在 jsfiddle 中分开,否则用于关闭父元素的 jQuery 脚本不起作用

转载 作者:行者123 更新时间:2023-11-30 07:31:20 25 4
gpt4 key购买 nike

我的网站上有一个横幅,观众应该可以通过按钮将其关闭。

我已经使用我在 stackoverflow 上找到的脚本解决了这个问题(谢谢!),并根据我的需要对其进行了定制。

问题是脚本只有在 JSfiddle 中分离到 Javascript+jQuery 左下角的框中时才有效。一旦我把它放在页眉或正文中,它就不再起作用了。

这一定是我遗漏的一些简单问题,比如语法问题,但我无法弄清楚,因为我对 CSS/HTML 以外的任何东西都不熟悉。

有人可以帮我吗?

非常感谢!!

阿尔法

.banner {
overflow: hidden;
background-color: #333;
position: fixed;
top: 0;
left: 0;
width: 100vw;
height: 100px;
opacity: 0.6;
color: #ffffff;
text-align: center;
font-family: 'Varela Round', sans-serif;
font-size: 13px;
padding-top: 5px;
}

.close {
color: #fff;
filter: alpha(opacity=20);
font-size: 20px;
font-weight: bold;
line-height: 1;
opacity: .8;
}

.close:hover,
.close:focus {
color: #fff;
cursor: pointer;
filter: alpha(opacity=50);
opacity: 1;
text-decoration: none;
}

button.close {
background: transparent;
border: 0;
cursor: pointer;
padding: 0;
-webkit-appearance: none;
-moz-appearance: none;
}
<html>

<head>

</head>

<body>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$('.close').on("click", function() {
$(this).parents('div').fadeOut();
});
</script>

<div class="banner well notice bg-green">


Close this banner <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
</div>
</body>

</html>

最佳答案

你应该用匿名 jquery 函数包装你的 js 代码,无论你把代码放在哪里,都要在页面竞争加载后加载,如下所示:

<script>
$(function() {
$('.close').on("click", function() {
$(this).parents('div').fadeOut();
});
});
</script>

关于javascript - 除非在 jsfiddle 中分开,否则用于关闭父元素的 jQuery 脚本不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51847038/

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