jquery在html文件中没有工作

Nic*_*nay 3 html javascript jquery

我正在尝试做一个非常基本的jquery教程,但我无法让它工作.我正在从谷歌调用jquery库,然后我尝试在html中创建一个脚本.

如果我在.js文件中执行相同的操作,我就不会有任何问题.我在这里错过了什么?

<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    </head>
    <body>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js">
            $(document).ready(function() {
                $("a").click(function() {
                    alert("Hello world!");
                });
            });
        </script>
            <a href="">Link</a>

    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

Mat*_*ell 10

你需要拆分它:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js">
    $(document).ready(function() {
        $("a").click(function() {
            alert("Hello world!");
        });
    });
</script>
Run Code Online (Sandbox Code Playgroud)

...分为两个脚本元素:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
        $("a").click(function() {
            alert("Hello world!");
        });
    });
</script>
Run Code Online (Sandbox Code Playgroud)

在您提供的代码段中,<script>元素中的代码将不会被评估,因为浏览器仅评估src属性中的内容并忽略其他所有内容.

  • 没问题.我们都去过那儿.:) (2认同)