为什么这个非常基本的JS下拉菜单不起作用
Why is this very basic JS dropdown menu not working?
它在Codepen中工作,但在本地不工作。我在同一目录中有html、css、js文件,我正在导入它们,正如您在html片段中看到的那样。有人建议我的js文件路径可能不正确,但js文件和其他文件在同一个目录中,我可以用同样的方式调用cs文件,这很有效。
我唯一能想到的是,我正在导入菜单脚本所需的不正确的jquery和jquery ui文件?
所以我的问题是为什么这个例子在这里有效http://codepen.io/WhiteWolfWizard/pen/DwKjc,但不是在我的本地机器上?
HTML
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.1/jquery-ui.min.js"></script>
<script type="text/javascript" src="menuscript.js"></script>
<div class='dropdown'>
<h1 class='title'>Select</h1>
<ol class='drop'>
<li>Apple</li>
<li>Orange</li>
<li>Banana</li>
<li>Pineapple</li>
<li>Mango</li>
</ol>
</div>
CSS
.dropdown {
position: relative;
margin: 50px auto;
display: table;
font-family: sans-serif;
font-size: 11pt;
color: #FC3;
line-height: normal;
text-align: left;
font-smoothing: antialiased;
}
.dropdown .title {
cursor: pointer;
height: 45px;
padding: 0 20px;
border-radius: 5px;
display: block;
box-shadow: 0 0 0 1px #FC3;
line-height: 45px;
}
.dropdown .title:after {
content: '';
float: right;
width: 0;
height: 0;
margin: 20px 0 0 20px;
border-width: 5px 4px 0;
border-style: solid;
border-color: #FC3 transparent transparent transparent;
}
.dropdown .drop {
position: relative;
top: 100%;
margin-top: 1px;
border-radius: 0 0 5px 5px;
display: none;
overflow: hidden;
box-shadow: 0 0 0 1px #FC3;
}
.dropdown .drop li {
cursor: pointer;
padding: 10px;
}
.dropdown .drop li:hover {
background: #FC3;
color: #FFF;
}
.select .title {
border-radius: 5px 5px 0 0;
}
.select .title:after {
transform: rotate(180deg);
}
JS
$('.dropdown .title').on('click',function(){
$(this).parent().toggleClass('select').find('.drop').toggle();
});
$('.dropdown .drop li').on('click',function(){
var $this = $(this), input = $this.text();
$('.dropdown .title').text(input);
});
enter code here
http://codepen.io/WhiteWolfWizard/pen/DwKjc
js需要在元素可用时运行。用$(函数(){})包装代码:
$(function () {
$('.dropdown .title').on('click',function(){
$(this).parent().toggleClass('select').find('.drop').toggle();
});
$('.dropdown .drop li').on('click',function(){
var $this = $(this), input = $this.text();
$('.dropdown .title').text(input);
});
});
相关文章:
- JS在选择下拉菜单打开时创建延迟
- JS下拉菜单没有'不起作用
- JS下拉菜单显示在图像旋转器上
- 使用 d3.js 添加下拉菜单
- HTML、CSS、JS响应下拉菜单
- 使用jQuery动态设计js下拉菜单的样式
- JS下拉菜单未关闭
- 如何在我的js下拉菜单/显示内容框上的不同内容之间切换
- 为什么我的css/js下拉菜单在到达它的末尾之前就隐藏了
- 为什么这个非常基本的JS下拉菜单不起作用
- Ember.js中的Foundation.js下拉菜单
- Angular JS下拉菜单使用ng点击和ng显示来显示动态内容
- 平板电脑/手机上JS下拉菜单的关闭按钮
- JS下拉菜单中的链接不会重定向
- Javascript,如何调整这个js下拉菜单
- 当我将鼠标悬停在项目上时,我的CSS/JS下拉菜单不会保持在下
- JS下拉菜单行为
- Knockout Js下拉菜单没有加载来自WebAPi Service的值
- CSS vs. Javascript(或库js)下拉菜单
- 使用js下拉菜单预加载图像