猿问

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

我的代码在 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


慕虎7371278
浏览 174回答 1
1回答

青春有我

当您<script type="text/javascript" src="./index.js"></script>在 jQuery 脚本之前添加jQuery 脚本时,您正面临此问题。将其移动到页面末尾应该可以解决问题。JavaScript 的执行按顺序发生。在这种情况下,首先执行 index.js 的内容,然后执行 jQuery。当一个脚本包含对另一个脚本的依赖时,它们被包含的顺序会有所不同。它们应该按照其依赖关系的顺序包含在内。
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答