jquery .click href

时间:2010-12-16 16:46:00

标签: jquery href

任何人都知道它为什么不起作用?

<script src="http://code.jquery.com/jquery-1.4.4.js"></script>
<script>
    $('#main').click(function() {
    alert('foobar');
    document.location.href='02.html';  
    });

</script>
<style type="text/css">
    body {margin:0px; background:#f2f2f2;}
    #main {background:url(01.jpg) top center no-repeat; height:1745px; width:100%; text-   align:center; overflow-x:hidden; cursor:pointer; cursor:hand;}
</style>
</head>
<body>
    <div id="main"></div>
</body>

请帮助

1 个答案:

答案 0 :(得分:14)

将点击功能包裹在文档就绪功能中,或将代码放在页面底部。

执行脚本时#main元素不存在。

示例:

<script src="http://code.jquery.com/jquery-1.4.4.js"></script>
<script>
$(document).ready(function() { 
    $('#main').click(function() {

    document.location.href='02.html';
    });
});
</script>
相关问题