简体   繁体   English

未捕获的TypeError:$ .simpleWeather不是函数

[英]Uncaught TypeError: $.simpleWeather is not a function

I am trying to implement simpleWeather.js on my website. 我正在尝试在我的网站上实现simpleWeather.js

Unfortunately, when I place the html in my website, I get 不幸的是,当我将html放在我的网站中时,

Uncaught TypeError: $.simpleWeather is not a function 未捕获的TypeError:$ .simpleWeather不是函数

I am referencing simpleWeather.js at the top of my html: 我在html的顶部引用了simpleWeather.js:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.simpleWeather/3.1.0/jquery.simpleWeather.min.js"></script>

And it gets loaded on website: 它被加载到网站上:

在此处输入图片说明

It works perfectly on codepen.io though :/ Any ideas? 它可以在codepen.io上完美运行 ://有什么想法吗?

EDIT: 编辑:

My full code looks like this: 我的完整代码如下所示:

<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.simpleWeather/3.1.0/jquery.simpleWeather.min.js"></script>
<script type="text/javascript">
   //copied javascript from codepen.io
</script>
<style type="text/css">
   //copied css from codepen.io
</style>
//copied html from codepen.io

Looks to me like you probably have your scripts in the wrong order. 在我看来,您的脚本顺序可能不正确。 Your script tag for jQuery must be before your script tag for simpleWeather. jQuery的script标签必须 simpleWeather的script标签之前 Then your script using simpleWeather must be after that. 然后,必须在此之后使用 simpleWeather 使用脚本。 (If you're using a script loader, you need to ensure that load order.) (如果使用脚本加载器,则需要确保加载顺序。)

Please check order of code... 请检查代码顺序...

You have to include library first then you can access that $.simpleWeather() method. 您必须先包含库,然后才能访问该$ .simpleWeather()方法。 code order something like this. 代码订购这样的东西。

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.simpleWeather/3.1.0/jquery.simpleWeather.min.js"></script>
...
...
...
<script>
$.simpleWeather({
    zipcode: '76309',
    unit: 'f',
    success: function(weather) {
        html = '<h2>'+weather.city+', '+weather.region+'</h2>';
        html += '<img style="float:left;" width="125px" src="images/weather/'+weather.code+'.png">';
        html += '<p>'+weather.temp+'&deg; '+weather.units.temp+'<br /><span>'+weather.currently+'</span></p>';
        html += '<a href="'+weather.link+'">View Forecast &raquo;</a>';

        $("#weather").html(html);
    },
    error: function(error) {
        $("#weather").html("<p>"+error+"</p>");
    }
});
</script>

Try it like this: 像这样尝试:

jQuery(document).ready(function ($) {
    // your code
});

Also, write the script tag after html and css 另外,在htmlcss之后写script标签

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

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