简体   繁体   English

您如何使javascript / jquery正常工作?

[英]How do you get javascript/jquery to work?

EDIT: Thanks for the suggestions everyone. 编辑:谢谢大家的建议。 I've gotten it squared away thanks to your help! 多亏了您的帮助,我才得以解决!

I am doing javascript/jquery tutorials on codeschool/codeacademy and everything seems to go fine there. 我在codeschool / codeacademy上进行javascript / jquery教程,那里似乎一切正常。 But when I try to do something myself, I cannot get the javascript to actually trigger. 但是,当我尝试自己做某事时,我无法使JavaScript真正触发。 I tried chrome and firefox. 我尝试了chrome和Firefox。 I disabled all plugin extentions. 我禁用了所有插件扩展。 Must be a simple problem, please help! 必须是一个简单的问题,请帮忙!

The following code I cut and pasted from jquery ( http://learn.jquery.com/about-jquery/how-jquery-works/ ) 我从jquery剪切和粘贴的以下代码( http://learn.jquery.com/about-jquery/how-jquery-works/

Here is a link to the jsfiddle: http://jsfiddle.net/interestinall/n5mqryrj/ 这是jsfiddle的链接: http : //jsfiddle.net/interestinall/n5mqryrj/

<!doctype html>
<html>
<head>
    <meta charset="utf-8" />
    <title>Demo</title>
</head>
<body>
    <a href="http://jquery.com/">jQuery</a>
    <script src="jquery.js"></script>
    <script>

    $( document ).ready(function() {
        $( "a" ).click(function( event ) {
            alert( "The link will no longer take you to jquery.com" );
            event.preventDefault();
        });
    });

    </script>
</body>
</html>

It does not work for me. 它对我不起作用。 Can someone explain why and tell me how to get it working? 有人可以解释一下原因,并告诉我如何使其工作吗? Thanks so much! 非常感谢!

You did not include jquery in your project. 您没有在项目中包含jquery。 change this line 改变这条线

<script src="jquery.js"></script>

to this line 到这条线

<script src="https://code.jquery.com/jquery-1.11.1.min.js" ></script>

or any other version in 或任何其他版本

https://code.jquery.com/ https://code.jquery.com/

Theres nothing wrong with your code. 您的代码没有错。 We were all started somewhere and some of these suggestions are misleading 我们都是从某个地方开始的,其中一些建议令人误解

If you're not already aware, jQuery is a comprehensive library which makes complex javascript development simpler. 如果您还不了解,jQuery是一个全面的库,可简化复杂的javascript开发。 Its important to note that everything you do with jQuery can be achieved using native javascript its just a little tricker and jquery handles all the complexities to work across a wide range of browsers. 重要的是要注意,您使用jQuery所做的所有事情都可以使用本机javascript来实现,这只是一点点欺骗,而jquery可以处理所有复杂性,从而可以在各种浏览器中使用。 That is to say some things work in some browsers, where as others do things in a different way (notably IE). 也就是说,某些功能可以在某些浏览器中运行,而其他浏览器则以其他方式(特别是IE)运行。

To use jQuery on your website, you need to include it on your page. 要在您的网站上使用jQuery,您需要将其包含在页面中。 Its self executing and one way you can tell if its worked is running something like: 它的自我执行能力和一种可以判断其是否正常运行的方式是:

<!doctype html>
<html>
<head>
    <meta charset="utf-8" />
    <title>Demo</title>
</head>
<body>
    <script src="https://code.jquery.com/jquery-1.11.1.min.js" ></script>
    <script>
        console.log($)
    </script>
</body>
</html>

If you open the console in chrome (hit f12) you should see: 如果您在Chrome中打开控制台(按f12键),则应看到:

function (a,b){return new m.fn.init(a,b)}

Note that I'm using src="https://code.jquery.com/jquery-1.11.1.min.js" for the source of the script. 请注意,我使用src="https://code.jquery.com/jquery-1.11.1.min.js"作为脚本的来源。 You can use src="jquery.js" as per your script, but it means you need to save the contents of https://code.jquery.com/jquery-1.11.1.min.js to a file called jquery.js at the same level as your index.html (or the wherever-you-saved-your-code.html) 您可以根据脚本使用src="jquery.js" ,但这意味着您需要将https://code.jquery.com/jquery-1.11.1.min.js的内容保存到名为jquery的文件中。 js与index.html(或您保存的your-code.html所在位置)处于同一级别

Ive noted some suggestions say you should include jQuery in the tag and, though this would work, its considered best practice to keep scripts at the bottom of the page for reasons I wont go into here. Ive指出了一些建议,说您应该在标记中包含jQuery,尽管这样做可以,但是出于我不愿在此介绍的原因,将脚本保留在页面底部的最佳实践被认为是最佳做法。

As such, this should work for you (irregardless of browser extensions): 因此,这应该对您有效(与浏览器扩展无关):

<!doctype html>
<html>
<head>
    <meta charset="utf-8" />
    <title>Demo</title>
</head>
<body>
    <a href="http://jquery.com/">jQuery</a>
    <script src="https://code.jquery.com/jquery-1.11.1.min.js" ></script>
    <script>

    $( document ).ready(function() {
        $( "a" ).click(function( event ) {
            alert( "The link will no longer take you to jquery.com" );
            event.preventDefault();
        });
    });

    </script>
</body>
</html>

Good luck. 祝好运。

You've used the wrong URL to jquery. 您对jquery使用了错误的URL。 http://fiddle.jshell.net/_display/jquery.js gives a 404 error. http://fiddle.jshell.net/_display/jquery.js出现404错误。

Try using an absolute URI or picking the library from the Frameworks & Extensions menu. 尝试使用绝对URI或从Frameworks&Extensions菜单中选择库。

You haven't included your jQuery properly. 您尚未正确包含jQuery。 Try this instead: 尝试以下方法:

<!doctype html>
<html>
<head>
    <meta charset="utf-8" />
    <title>Demo</title>
    <script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
</head>
<body>
    <a href="http://jquery.com/">jQuery</a>    
    <script>

        $( document ).ready(function() {
            $( "a" ).click(function( event ) {
                alert( "The link will no longer take you to jquery.com" );
                event.preventDefault();
            });
        });

    </script>
</body>
</html>

Try to replace 尝试更换

<a href="http://jquery.com/">jQuery</a>

with

<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Or download the jQuery-Scriptfile from jQuery.com, store it locally and use that one. 或从jQuery.com下载jQuery-Scriptfile,将其存储在本地并使用该文件。

Here is a working example http://jsbin.com/pajecubute/1/edit 这是一个工作示例http://jsbin.com/pajecubute/1/edit

<a href="http://jquery.com/">jQuery</a>

$( document ).ready(function() {
    $( "a" ).click(function( event ) {
        alert( "The link will no longer take you to jquery.com" );
        event.preventDefault();
    });
});

Updated your fiddle. 更新了您的小提琴。 You need not include JQuery separately in your HTML. 您无需在HTML中单独包含JQuery。 It is already present for your selection on the left hand side. 您已经可以在左侧找到它。 Your code works now. 您的代码现在可以使用了。

http://jsfiddle.net/n5mqryrj/8/ http://jsfiddle.net/n5mqryrj/8/

Added ready function in the script section: script部分添加了ready函数:

$( document ).ready(function() {
    $( "a" ).click(function( event ) {
        alert( "The link will no longer take you to jquery.com" );
        event.preventDefault();
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM