这是我的下拉菜单 html。最初,在 CSS 中显示菜单:无; .
<!doctype html>
<html>
<head>
<title>DropDown Menu</title>
<link rel="stylesheet" href="normalize.css">
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>DropDown Menu</h1>
<div id="menuButton" >Menu Button</div>
<div>
<ul id="dropdown">
<li>
<a href=""></a>Home</a>
</li>
<li>
<a href=""></a>Services</a>
</li>
<li>
<a href=""></a>About</a>
</li>
<li>
<a href=""></a>Contact</a>
</li>
<li>
<a href=""></a>Partners</a>
</li>
</ul>
</div>
<!-- Load the CDN first -->
<script src="//code.jquery.com/jquery-2.1.4.min.js"></script>
<!-- If CDN fails to load, serve up the local version -->
<script>window.jQuery || document.write('<script src="js/jquery-2.1.4.js"><\/script>');</script>
<script src="js/script.js"></script>
</body>
</html>
这是我的 jQuery 脚本:
// script.js
$(function () {
$( "#menuButton" ).click(function() {
$( "#dropdown" ).slideToggle();
});
});
上面的 jQuery 不起作用。但是,如果在 div 中添加一个 <p>
带有 id= menuButton 的标签,它工作得很好。谁能帮我解决这个问题?我已经挣扎了一段时间。
最佳答案
关于javascript - 出于某种原因,我的 .click 方法不适用于 <div id=menuButton>。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34940799/