【问题标题】:jquery .click hrefjquery .click 链接
【发布时间】:2011-11-21 17:01:35
【问题描述】:

有人知道为什么它不起作用吗?

<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>

请帮忙

【问题讨论】:

    标签: jquery href


    【解决方案1】:

    将点击函数封装在文档就绪函数中,或将代码放在页面底部。

    执行脚本时,#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>
    

    【讨论】:

    • 这是我不喜欢 jQuery 的一件事——当您不选择任何对象时,它会返回一个有效但为空的对象。我知道这个功能很有用,但对初学者来说也很粗糙并且难以调试。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-04
    • 1970-01-01
    • 2018-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-03
    相关资源
    最近更新 更多