2014-10-17 107 views
0

它在Codepen中有效,但它在本地不起作用。我在同一个目录中有html,css,js文件,我正在导入它们,正如你在html代码片段中看到的一样。有人建议,我的js文件路径可能不正确,但js文件与其他所有文件位于相同的目录中,我可以用相同的方式调用我的cs文件并且工作正常。为什么这个非常基本的JS下拉菜单不起作用?

我能想到的唯一的事情就是我导入了我的menuscript需要的不正确的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

+0

检查控制台是否有错误 – Joseph 2014-10-17 00:37:21

回答

1

JS需要时的元素都可以运行。将代码封装在$(function(){})中:

$(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); 
    }); 
});