我有这个脚本:
$("#test").click(function() {
alert('hue');
});
Run Code Online (Sandbox Code Playgroud)
我的html上有这个元素:
<li>
<a id="test" href="#contato">Contato</a>
</li>
Run Code Online (Sandbox Code Playgroud)
我已经导入了我的滚动脚本和jQuery.js脚本.
<script src="js/scrolling.js"></script>
<script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
Run Code Online (Sandbox Code Playgroud)
但是当我点击我的元素时,没有警报()!我究竟做错了什么?我怎样才能解决这个问题?
小智 5
我假设你的意思是js/scrolling.js包含
$("#test").click(function() {
alert('hue');
});
Run Code Online (Sandbox Code Playgroud)
在这种情况下,您需要scrolling.js在jQuery之后包含.如果这些脚本标记位于<head>您的html中,则需要包含DOM-ready函数,因为在页面上存在元素之前无法绑定它.
$(document).ready(function() {
$("#test").click(function() {
alert('hue');
});
});
Run Code Online (Sandbox Code Playgroud)