为了账号安全,请及时绑定邮箱和手机立即绑定

如何修复我无响应的删除 div 功能?

如何修复我无响应的删除 div 功能?

慕虎7371278 2021-09-30 17:37:06
我的代码在 codepen(下面的链接)中完美运行,但由于某种原因在浏览器中它不起作用。我正在从 localhost 运行代码,但按钮没有响应。代码这是我的 VS 代码jQuery(function($) {  $('.removeDiv').on('click', function() {    $(this).closest('#cookieNotice').remove();  });});<html><head>  <!-- Required meta tags -->  <meta charset="utf-8">  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">  <!-- Bootstrap CSS -->  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">  <!-- Font Awesome CSS -->  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.10.2/css/all.min.css">  <!-- Index Javascript-->  <script type="text/javascript" src="index.js"></script>  <title>Urabiliti Home Page</title></head><body>  <!-- Footer -->  <footer class="page-footer font-small blue fixed-bottom bg-light" id="cookieNotice">    <!-- Copyright -->    <div class="footer-copyright text-center py-3">This website uses      <a href="https://mdbootstrap.com/education/bootstrap/"> cookies</a> to ensure you get the best experience on our website    </div>    <!-- remove cookie notice -->    <div class="removeDiv text-center">      <button type="button" class="btn btn-primary mb-4">Got it</button>    </div>展开片段我希望 div 被删除 onclick
查看完整描述

1 回答

?
青春有我

TA贡献1784条经验 获得超8个赞

当您<script type="text/javascript" src="./index.js"></script>在 jQuery 脚本之前添加jQuery 脚本时,您正面临此问题。将其移动到页面末尾应该可以解决问题。

JavaScript 的执行按顺序发生。在这种情况下,首先执行 index.js 的内容,然后执行 jQuery。

当一个脚本包含对另一个脚本的依赖时,它们被包含的顺序会有所不同。它们应该按照其依赖关系的顺序包含在内。


查看完整回答
反对 回复 2021-09-30
  • 1 回答
  • 0 关注
  • 173 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信