jQuery Animation在IE9& Firefox 5

时间:2011-08-31 13:59:08

标签: javascript jquery jquery-animate

这是来源,我错过了什么吗? 我尝试了here,它有效,但在我的电脑上却不能正常工作 - 编辑
我使用了jquery min v1.4.1,它没有用,所以我下载了最新版本,仍然无法正常工作,我使用google的jquery SDN即v1.5.1链接。

<html>
    <head>
    <title>Test</title>
    <script src="jquery-1.6.2.min.js" type="text/javascript"></script>
    <script type="text/javascript">
    $('document').ready(function() {
        $('#bio > div').hide();
        $('#bio > div:first').show();
    });
    $('#bio h3').click(function() {
        alert('called');
        $(this).next().animate(
            {'height':'toggle'}, 'slow', 'swing'
        );
    });
    $('p:first').animate(
        {
        height: '+=100px',
        backgroundColor: 'green'
        },
        {
            duration: 'slow',
            easing: 'swing',
            complete: function() {alert('done!');},
            queue: false
        }
    );
    </script>
    </head>
    <body>

        <div id="bio">
            <h2>Who’s Hot Right Now?</h2>
            <h3>Beau Dandy</h3>
            <div>
                <img src="../images/beau_100.jpg" width="100"
                    height="100" alt="Beau Dandy"/>
                <p>Content about Beau Dandy</p>
            </div>
            <h3>Johnny Stardust</h3>
            <div>
                <img src="../images/johnny_100.jpg" width="100"
                height="100" alt="Johny Stardust"/>
                <p>Content about Johny Stardust</p>
            </div>
            <h3>Glendatronix</h3>
            <div>
                <img src="../images/glenda_100.jpg" width="100"
                    height="100" alt="Glendatronix"/>
                <p>Content about Glendatronix</p>
            </div>
        </div>
    </body>
</html>

1 个答案:

答案 0 :(得分:1)

我认为你的document.ready过早关闭了。

我只是将括号});移到了脚本的末尾......

<script type="text/javascript">
$('document').ready(function() {
    $('#bio > div').hide();
    $('#bio > div:first').show();
    $('#bio h3').click(function() {
        alert('called');
        $(this).next().animate(
            {'height':'toggle'}, 'slow', 'swing'
        );
    });
    $('p:first').animate(
        {
        height: '+=100px',
        backgroundColor: 'green'
        },
        {
        duration: 'slow',
        easing: 'swing',
        complete: function() {alert('done!');},
        queue: false
        }
    );
});
</script>

为什么呢?例如,您尝试将.click()之类的事件绑定到名为#bio h3的元素。但是,由于您在#bio h3中调用脚本,因此元素<head>可能尚未存在于DOM中。使用document.ready可确保在执行代码之前存在DOM。

为什么它在某些浏览器中有效可能是一个简单的计时问题。