如何在浏览器上运行JQuery代码?

时间:2013-04-24 23:10:06

标签: javascript jquery html

我是JQuery的新手,我正在试图弄清楚如何使我的.mouseenter()和.mouseleave()方法起作用。到目前为止,我已经尝试过使用IE8和FF,但由于一些奇怪的原因,除了保持静态之外,我无法让我的元素做任何事情。这是我到目前为止的代码:

HTML:

<!Doctype html>
<html>
    <head>
        <link type="text/css" rel="stylesheet" href="style.css"/>
        <script type="text/javascript" src="file:///D:/Documents%20and%20Settings/stsc/My%20Documents/_prac/script.js"></script>
        <title>Practice</title>
    </head>
    <body>
        <div id="red"></div>
        <div id="yellow"></div>
        <div id="green"></div>
    </body>
</html>

CSS:

div{
    height:100px;
    width: 100px;
    border-radius: 50px;
}

#red{
    background-color: red;
}

#yellow{
    background-color: yellow;
}

#green{
    background-color: green;
}

JS:

$(document).ready(function(){
    $('div').mouseenter(function(){
        $(this).animate({
            width: '+=10px'
        });
    });
    $('div').mouseleave(function(){
        $(this).animate({
            width: '-=10px'
        });
    });
    $('div').click(function() {
        $(this).toggle(1000);
    });
});

这只是练习使用JQuery的一个简单示例。在此先感谢帮助人员!

2 个答案:

答案 0 :(得分:2)

你好像不包括jQuery:

<script src="http://code.jquery.com/jquery-1.9.1.js"></script>

此外,D:\驱动器是硬盘驱动器还是CD / DVD驱动器?这也可能是一个问题。

答案 1 :(得分:1)

您应该在HTML中引用jquery库:

<head>
    <link type="text/css" rel="stylesheet" href="style.css"/>
    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script type="text/javascript" src="file:///D:/Documents%20and%20Settings/stsc/My%20Documents/_prac/script.js"></script>
    <title>Practice</title>
</head>