如何一键调用三个位置上javascript

发布于 2025-01-17 18:44:09 字数 3974 浏览 3 评论 0原文

我正在尝试弄清楚如何使用JavaScript和开放天气来获取3个不同位置的3张卡的代码。 目前,我为JavaScript提供了这个。

function checkWeather() {
    var settings ={
        "async": true,
        "crossDomain": true,
        "dataType": "json",
        "url": "http://api.openweathermap.org/data/2.5/weather?zip=82190,us&appid=<API>&units=imperial",
        "method": "GET"
    };
    $.ajax(settings)

    .done(function (response) {
        console.log(response);

        $("#wind_speed").append (response.wind.speed);
        $("#main_temp").append (response.main.temp);
        $("#weather_conditions").append (response.weather[0].main);
        $("#wind_speed_unit").append (" MPH");
        $("#main_temp_unit").append (" F");
    });
}
function checkWeather() {
    var settings ={
        "async": true,
        "crossDomain": true,
        "dataType": "json",
        "url": "http://api.openweathermap.org/data/2.5/weather?zip=44405,us&appid=<API>&units=imperial",
        "method": "GET"
    };
    $.ajax(settings)

    .done(function (response) {
        console.log(response);

        $("#wind_speed1").append (response.wind.speed);
        $("#main_temp1").append (response.main.temp);
        $("#weather_conditions1").append (response.weather[0].main);
        $("#wind_speed_unit").append (" MPH");
        $("#main_temp_unit").append (" F");
    });
}
function checkWeather() {
    var settings ={
        "async": true,
        "crossDomain": true,
        "dataType": "json",
        "url": "http://api.openweathermap.org/data/2.5/weather?zip=44512,us&appid=<API>&units=imperial",
        "method": "GET"
    };
    $.ajax(settings)

    .done(function (response) {
        console.log(response);

        $("#wind_speed2").append (response.wind.speed);
        $("#main_temp2").append (response.main.temp);
        $("#weather_conditions2").append (response.weather[0].main);
        $("#wind_speed_unit").append (" MPH");
        $("#main_temp_unit").append (" F");
    });
}

对于HTML,

        <h2>Top National Parks Weather</h2>
        Check Weather
       
        <div class="card level-3 row" id="0">
        
        <h2>Yosemite National Park</h2>
        <span><b>Temperature: </b></span><span id="main_temp"></span><span id="main_temp_unit"></span><br />
        <span><b>Wind speed: </b></span><span id="wind_speed"></span> <span id="wind_speed_unit"></span><br />
        <span><b>Current conditions: </b></span><span id="weather_conditions"></span>
    </div>
    <div class="card level-3 row" id="1">
         
        <h2>Weather</h2>
        <span><b>Temperature: </b></span><span id="main_temp1"></span><span id="main_temp_unit"></span><br />
        <span><b>Wind speed: </b></span><span id="wind_speed1"></span> <span id="wind_speed_unit"></span><br />
        <span><b>Current conditions: </b></span><span id="weather_conditions1"></span>
    </div>
    <div class="card level-3 row" id="2">
       
        <h2>Weather</h2>
        <span><b>Temperature: </b></span><span id="main_temp2"></span><span id="main_temp_unit"></span><br />
        <span><b>Wind speed: </b></span><span id="wind_speed2"></span> <span id="wind_speed_unit"></span><br />
        <span><b>Current conditions: </b></span><span id="weather_conditions2"></span>
    </div>
    </div>

我尝试执行三组JS,但它与更改变量无效。这样做时,它会更改最后一张卡,并且不允许其他三个中的任何一个运行。我希望能够拉3个特定位置,然后在点击时显示当前的天气。我需要添加一个活动侦听器吗?

I am trying to figure out how to get the code to populate weather for 3 cards for 3 different locations using javascript and open weather.
Currently, I have this for javascript.

function checkWeather() {
    var settings ={
        "async": true,
        "crossDomain": true,
        "dataType": "json",
        "url": "http://api.openweathermap.org/data/2.5/weather?zip=82190,us&appid=<API>&units=imperial",
        "method": "GET"
    };
    $.ajax(settings)

    .done(function (response) {
        console.log(response);

        $("#wind_speed").append (response.wind.speed);
        $("#main_temp").append (response.main.temp);
        $("#weather_conditions").append (response.weather[0].main);
        $("#wind_speed_unit").append (" MPH");
        $("#main_temp_unit").append (" F");
    });
}
function checkWeather() {
    var settings ={
        "async": true,
        "crossDomain": true,
        "dataType": "json",
        "url": "http://api.openweathermap.org/data/2.5/weather?zip=44405,us&appid=<API>&units=imperial",
        "method": "GET"
    };
    $.ajax(settings)

    .done(function (response) {
        console.log(response);

        $("#wind_speed1").append (response.wind.speed);
        $("#main_temp1").append (response.main.temp);
        $("#weather_conditions1").append (response.weather[0].main);
        $("#wind_speed_unit").append (" MPH");
        $("#main_temp_unit").append (" F");
    });
}
function checkWeather() {
    var settings ={
        "async": true,
        "crossDomain": true,
        "dataType": "json",
        "url": "http://api.openweathermap.org/data/2.5/weather?zip=44512,us&appid=<API>&units=imperial",
        "method": "GET"
    };
    $.ajax(settings)

    .done(function (response) {
        console.log(response);

        $("#wind_speed2").append (response.wind.speed);
        $("#main_temp2").append (response.main.temp);
        $("#weather_conditions2").append (response.weather[0].main);
        $("#wind_speed_unit").append (" MPH");
        $("#main_temp_unit").append (" F");
    });
}

and this for HTML

        <h2>Top National Parks Weather</h2>
        Check Weather
       
        <div class="card level-3 row" id="0">
        
        <h2>Yosemite National Park</h2>
        <span><b>Temperature: </b></span><span id="main_temp"></span><span id="main_temp_unit"></span><br />
        <span><b>Wind speed: </b></span><span id="wind_speed"></span> <span id="wind_speed_unit"></span><br />
        <span><b>Current conditions: </b></span><span id="weather_conditions"></span>
    </div>
    <div class="card level-3 row" id="1">
         
        <h2>Weather</h2>
        <span><b>Temperature: </b></span><span id="main_temp1"></span><span id="main_temp_unit"></span><br />
        <span><b>Wind speed: </b></span><span id="wind_speed1"></span> <span id="wind_speed_unit"></span><br />
        <span><b>Current conditions: </b></span><span id="weather_conditions1"></span>
    </div>
    <div class="card level-3 row" id="2">
       
        <h2>Weather</h2>
        <span><b>Temperature: </b></span><span id="main_temp2"></span><span id="main_temp_unit"></span><br />
        <span><b>Wind speed: </b></span><span id="wind_speed2"></span> <span id="wind_speed_unit"></span><br />
        <span><b>Current conditions: </b></span><span id="weather_conditions2"></span>
    </div>
    </div>

I tried to do three sets of js but it did not work with changing variables. When doing so it changes the last card and does not allow any of the other three to function. I would like to be able to pull 3 specific locations and then be able to display the current weather upon the click. Do I need to add an event listener?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文