javascript - 为什么我的 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一样。
有什么问题?
解决方案
尝试将<script>
移到之前</body>
或将其包装在附加的$()
中,这是 jQuery 的快捷方式$(document).ready()
推荐阅读
- mongodb - 如何在 Mongo 的“根”上向数组中添加元素
- javascript - d3js donut chart - highlight first data
- java - 如何在多阶段 docker build 中使用 maven 本地存储库?
- java - 代码在return语句后继续执行
- solr - solr: ltr (reranking) 结合 cursorMarks
- r - 如何为闪亮的应用程序预设小数点分隔符
- c# - 如何在 Visual Studio 项目中初始化自动映射器
- shady - osx 重新安装后,Shady 因 SyntaxError 失败
- html - 星夜动画的关键帧
- c - 创建一个游戏板