Javascript 如何获取通过API动态生成的div中的值?

Javascript 如何获取通过API动态生成的div中的值?,javascript,jquery,api,Javascript,Jquery,Api,基本上,当前天气通过天气API显示在我们的页面上 我想做的是捕获数据(比如29摄氏度),并使用javascript为其创建条件。比如,如果温度是25-29摄氏度,就会出现一个阳光明媚的图标(否则,应该显示另一个图标) 但是,当我试图捕获数据时,控制台会显示“空字符串”,尽管数据在页面上可见。如何进行此操作以捕获动态数据 <div class="weather"> [DYNAMICALLY WEATHER IS DISPLAYED HERE] </div> $(d

基本上,当前天气通过天气API显示在我们的页面上

我想做的是捕获数据(比如29摄氏度),并使用javascript为其创建条件。比如,如果温度是25-29摄氏度,就会出现一个阳光明媚的图标(否则,应该显示另一个图标)

但是,当我试图捕获数据时,控制台会显示“空字符串”,尽管数据在页面上可见。如何进行此操作以捕获动态数据

<div class="weather">
[DYNAMICALLY WEATHER IS DISPLAYED HERE]
</div>    

$(document).ready(function() {
      $.simpleWeather({
        location: 'Singapore, SG',
        woeid: '',
        unit: 'c',
        success: function(weather) {
          html = '<p class="widget-weather-info-temp"> '+weather.temp+'<sup class="widget-weather-deg">&deg;</sup><sup class="widget-weather-unit-temp">'+weather.units.temp+'</sup></p>';

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


    var $widgetInfoTemp = $(".widget-weather-info-temp").text();
    console.log($widgetInfoTemp);

[此处显示动态天气]
$(文档).ready(函数(){
$.SimpleWither({
地点:'新加坡,SG',
可悲的是:“,
单位:'c',
成功:功能(天气){
html='

'+weather.temp+'°;'+weather.units.temp+'

'; $(“#weather”).html(html); }, 错误:函数(错误){ $(“#weather”).html(“”+错误+”

”); } }); }); var$widgetInfoTemp=$(“.widget天气信息温度”).text(); log($widgetInfoTemp);
对API的调用是异步的,因此需要将
控制台.log()
放在
success
处理程序函数中。这样做时,您不需要从DOM请求元素,因为您已经可以访问返回的数据。试试这个:

$(document).ready(function() {
    $.simpleWeather({
        location: 'Singapore, SG',
        woeid: '',
        unit: 'c',
        success: function(weather) {
            html = '<p class="widget-weather-info-temp">' + weather.temp + '<sup class="widget-weather-deg">&deg;</sup><sup class="widget-weather-unit-temp">' + weather.units.temp + '</sup></p>';
            $("#weather").html(html);

            // work with the weather data here...
            console.log(weather);
            console.log(weather.temp);
            console.log(weather.units.temp);
        },
        error: function(error) {
            $("#weather").html('<p>' + error + '</p>');
        }
    });
});
$(文档).ready(函数(){
$.SimpleWither({
地点:'新加坡,SG',
可悲的是:“,
单位:'c',
成功:功能(天气){
html='

'+weather.temp+'°;'+weather.units.temp+'

'; $(“#weather”).html(html); //使用这里的天气数据。。。 控制台.日志(天气); 控制台日志(天气温度); 控制台日志(天气单位温度); }, 错误:函数(错误){ $(“#weather”).html(“”+错误+”

”); } }); });
对API的调用是异步的,因此需要将
控制台.log()
放在
success
处理程序函数中。这样做时,您不需要从DOM请求元素,因为您已经可以访问返回的数据。试试这个:

$(document).ready(function() {
    $.simpleWeather({
        location: 'Singapore, SG',
        woeid: '',
        unit: 'c',
        success: function(weather) {
            html = '<p class="widget-weather-info-temp">' + weather.temp + '<sup class="widget-weather-deg">&deg;</sup><sup class="widget-weather-unit-temp">' + weather.units.temp + '</sup></p>';
            $("#weather").html(html);

            // work with the weather data here...
            console.log(weather);
            console.log(weather.temp);
            console.log(weather.units.temp);
        },
        error: function(error) {
            $("#weather").html('<p>' + error + '</p>');
        }
    });
});
$(文档).ready(函数(){
$.SimpleWither({
地点:'新加坡,SG',
可悲的是:“,
单位:'c',
成功:功能(天气){
html='

'+weather.temp+'°;'+weather.units.temp+'

'; $(“#weather”).html(html); //使用这里的天气数据。。。 控制台.日志(天气); 控制台日志(天气温度); 控制台日志(天气单位温度); }, 错误:函数(错误){ $(“#weather”).html(“”+错误+”

”); } }); });
在html追加之后,在success函数中执行所有逻辑

 success: function(weather) {
          html = '<p class="widget-weather-info-temp"> '+weather.temp+'<sup class="widget-weather-deg">&deg;</sup><sup class="widget-weather-unit-temp">'+weather.units.temp+'</sup></p>';

          $("#weather").html(html);

    var $widgetInfoTemp = $(".widget-weather-info-temp").text();
    console.log($widgetInfoTemp);
        },
成功:功能(天气){
html='

'+weather.temp+'°;'+weather.units.temp+'

'; $(“#weather”).html(html); var$widgetInfoTemp=$(“.widget天气信息温度”).text(); log($widgetInfoTemp); },
在html追加之后,在success函数中执行所有逻辑

 success: function(weather) {
          html = '<p class="widget-weather-info-temp"> '+weather.temp+'<sup class="widget-weather-deg">&deg;</sup><sup class="widget-weather-unit-temp">'+weather.units.temp+'</sup></p>';

          $("#weather").html(html);

    var $widgetInfoTemp = $(".widget-weather-info-temp").text();
    console.log($widgetInfoTemp);
        },
成功:功能(天气){
html='

'+weather.temp+'°;'+weather.units.temp+'

'; $(“#weather”).html(html); var$widgetInfoTemp=$(“.widget天气信息温度”).text(); log($widgetInfoTemp); },