首页 > 解决方案 > 为什么我的 JavaScript 不能在 localhost 上运行?

问题描述

在 codepen https://codepen.io/mkliver/pen/oKbENd我得到了工作脚本。
当我在本地主机上复制此代码时,没有任何效果。我的代码:

<!DOCTYPE html>

<head>
  <meta charset="utf-8" />
  <title>Welcome to Foundation</title>

  <link rel="stylesheet" href="stylesheets/collapsable.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"> </script>

<script>
$('.tree .question').click( function() {
  $(this).parent().toggleClass('expanded').
  closest('li').find('ul:first').
  toggleClass('show-effect');
});
</script>
</head>
<body>
   <ul class="tree">

     <li class="tree__item hasChildren">
      <span>
        <div class="icon"></div>
        <a class="question">Question 1</a>
    </span>
    <ul>

        <li>
            <span><a href="#">Everything I seem to investigate lately seems to present itself with an annoying bug/feature in various browsers. Last time it was the inconsistency between browsers and generated content on form elements.</a></span>
        </li>

    </ul>

</li>

</ul>
</body>

collapsable.css和codepen一样。
有什么问题?

标签: javascriptjquery

解决方案


尝试将<script>移到之前</body>或将其包装在附加的$()中,这是 jQuery 的快捷方式$(document).ready()


推荐阅读