Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 从其他函数访问变量_Jquery_Html_Ajax_Scope - Fatal编程技术网

Jquery 从其他函数访问变量

Jquery 从其他函数访问变量,jquery,html,ajax,scope,Jquery,Html,Ajax,Scope,所以我做了一个来自freeCodeCamp的项目,在这个项目中我必须得到你的位置,并显示该位置的天气。一切都正常(通过反复试验使其正常工作),但是我不太明白为什么我可以在putInHtml函数中访问url变量,因为所有变量都是在getPosition函数中声明的 以下是所有代码: 函数getPosition(){ 变量lat、lon、url; if(导航器.地理位置){ navigator.geolocation.getCurrentPosition(showPosition); }否则{ 警

所以我做了一个来自freeCodeCamp的项目,在这个项目中我必须得到你的位置,并显示该位置的天气。一切都正常(通过反复试验使其正常工作),但是我不太明白为什么我可以在
putInHtml
函数中访问
url
变量,因为所有变量都是在
getPosition
函数中声明的

以下是所有代码:

函数getPosition(){ 变量lat、lon、url; if(导航器.地理位置){ navigator.geolocation.getCurrentPosition(showPosition); }否则{ 警报(“找不到位置”); } } 函数showPosition(pos){///paima objecta是getCurrentPosition 纬度=位置坐标纬度; lon=位置坐标经度; url=”https://fcc-weather-api.glitch.me/api/current?lat=“+lat+”&lon=“+lon; } getPosition(); 函数putInHtml(){ $.ajax({ 键入:“获取”, url:url, 数据类型:“json”, 成功:功能(数据){ $('#location').html(data.name); $('#temp').html(data.main.temp); $('#desc').html(data.weather[0].description) }, 错误:功能(请求、状态、错误){ console.log(错误); } }) } $(“#spust”)。单击(putInHtml)

天气应用程序


Spust
您的
lat
lon
url
变量不在其余函数的作用域内。您可以通过在函数外部声明变量来解决此问题,如下所示:

var lat, lon, url;
function getPosition(){
  //Removed your variables from here
  if(navigator.geolocation){
    navigator.geolocation.getCurrentPosition(showPosition);
  } else {
    alert('Cant find location');
  }
}
function showPosition(pos) { /// paima objecta is getCurrentPosition    
  lat = pos.coords.latitude;
  lon = pos.coords.longitude;   
  url = "https://fcc-weather-api.glitch.me/api/current?lat="+lat+"&lon="+lon;
}  
getPosition();

function putInHtml(){

  $.ajax({
    type:"GET",
    url: url,
    dataType:"json",
    success: function(data){

      $('#location').html(data.name);
      $('#temp').html(data.main.temp);
      $('#desc').html(data.weather[0].description)
    },
    error: function (request, status, error) {
          console.log(error);
    }
  })   
} 

$("#spust").click(putInHtml);

现在,变量将在所有函数的作用域中,因为我们已将它们添加到全局作用域中。阅读更多关于scope的信息,希望对您有所帮助

JavaScript作用域是基于函数的。
showPosition
函数是一个独立于
getPosition
函数的函数,它们是对等的;也就是说,
showPosition
没有嵌套在
getPosition
中,因此它对作用于
showPosition
的变量没有可见性。如果要使
showPosition
可见
url
,则必须在包含这两个函数的作用域中向上声明
url
。例如,全局作用域、函数(如)或包含这两个函数的ES6。或者您可以将
url
的声明更改为ES6,以引入封装这两个函数的块作用域。

在此函数中

function showPosition(pos) { /// paima objecta is getCurrentPosition    
 lat = pos.coords.latitude;
 lon = pos.coords.longitude;   
 url = "https://fcc-weather-api.glitch.me/api/current?lat="+lat+"&lon="+lon;
}  
url在全局范围内,因为您没有使用var

var url = "https://fcc-weather-api.glitch.me/api/current?lat="+lat+"&lon="+lon;

使用var声明它会将其范围限制在showPosition函数中,然后您将无法在该函数之外使用。

在任何函数之外定义全局变量,以便您可以在任何函数内部或外部访问或更新它:

 var url; //This makes the URL globally defined

function getPosition(){
      var lat, lon, url;
      if(navigator.geolocation){
        navigator.geolocation.getCurrentPosition(showPosition);
      } else {
        alert('Cant find location');
      }
    }
    function showPosition(pos) { /// paima objecta is getCurrentPosition    
      lat = pos.coords.latitude;
      lon = pos.coords.longitude;   
      url = "https://fcc-weather-api.glitch.me/api/current?lat="+lat+"&lon="+lon;
    }  
    getPosition();

    function putInHtml(){

      $.ajax({
        type:"GET",
        url: url,
        dataType:"json",
        success: function(data){

          $('#location').html(data.name);
          $('#temp').html(data.main.temp);
          $('#desc').html(data.weather[0].description)
        },
        error: function (request, status, error) {
              console.log(error);
        }
      })   
    } 

    $("#spust").click(putInHtml);

你确定你的代码笔上有完全相同的代码吗?因为它不应该起作用。
url
变量是在
showPosition
的作用域中定义的,因此在另一个函数/作用域中使用它应该是不可能的。我很确定,这里有一个指向codepen的链接:我最初的代码是在全局作用域中使用这些变量的,但是后来我尝试将这些变量添加到getPosition函数中,整个过程仍然在工作,因此我的问题是:为什么它会工作?谢谢,在我的初始代码中,将这些变量放在所有函数之外,但是当我尝试将它们放在getPosition函数中时,整个过程仍然有效,因此,我的问题是:它为什么工作?@BenasLengvinas如果您在getPosition函数中声明了这些变量,而不使用var关键字,JavaScript将自动将它们分配到全局范围。由于它们将在全局范围内分配,所有函数都可以访问它们。JS的许多陷阱之一:)这就是问题所在——我确实使用了var关键字进行了声明。下面是代码笔:虽然您确实在getPosition()函数中使用了var关键字进行声明,但这并不能单独将它们添加到全局范围。因此,当它到达showPosition()函数时,该函数无法看到这些变量,因为它们超出了范围。因此,由于您没有使用var关键字,它将自动将这些变量分配到showPosition()中的全局范围。一个主要的陷阱。我强烈建议你在scope上阅读一些文章/观看一些YT视频。这会让你大吃一惊的!了解这种语言,以及任何一种语言,都是一件非常好的事情当然可以祝您编码愉快,如果您有任何问题,请随时联系我。