如何多次调用javascript函数?
How to call javascript function multiple times?
我在我的网站上使用来自 OpenWeatherMap 的 API 来仅在一个页面中显示某些特定地点的天气状况。
这是代码:
<script src="https://code.jquery.com/jquery-3.0.0.js"></script>
<body>
<input type="hidden" name="name" value="dehradun" id="cityName">
<button onclick="getWeather()">Get weather of Dehradun</button>
<div class="weatherResponse"></div>
<br><br>
<input type="hidden" name="name" value="pune" id="cityName">
<button onclick="getWeather()">Get weather of Pune</button> '
<div class="weatherResponse"></div>
<br><br>
<script>
function getWeather() {
$('.weatherResponse').html('');
var cityName = $('#cityName').val();
var apiCall = 'http://api.openweathermap.org/data/2.5/weather?q=' + cityName + '&mode=json&units=metric&appid=<apikey>';
$.getJSON(apiCall, weatherCallback);
function weatherCallback(weatherData) {
var cityName = weatherData.name;;
var country = weatherData.sys.country;
var description = weatherData.weather[0].description;
var temp = weatherData.main.temp;
$('.weatherResponse').append("The Weather in " + cityName + " " + country + " is currently " + description + " with temperature: " + temp);
}
}
</script>
</body>
假设如果我想使用相同的 Javascript 函数显示两个地方的天气状况,那么它会为两个地方(代码中首先提到的地点(台拉登))显示相同的结果。
帮我解决这个问题。
我已经大致添加了下面的工作代码,当你点击一个按钮时,它会在两个文本中显示该城市的是否报告,尝试点击两个按钮看看:
<script src="https://code.jquery.com/jquery-3.0.0.js"></script>
<body>
<input type="hidden" name="name" value="dehradun" id="cityName2">
<button onclick="getWeather('dehradun')">Get weather of Dehradun</button>
<div class="weatherResponse"></div>
<br><br>
<input type="hidden" name="name" value="pune" id="cityName1">
<button onclick="getWeather('pune')">Get weather of Pune</button>
<br><br>
<div class="weatherResponse"></div>
<script>
function getWeather(cityName){
$('.weatherResponse').html('');
var apiCall= 'http://api.openweathermap.org/data/2.5/weather?q=' + cityName + '&mode=json&units=metric&appid=d3d913fca612366a61837ea39a622480';
$.getJSON(apiCall,weatherCallback);
function weatherCallback(weatherData){
var cityName= weatherData.name;;
var country= weatherData.sys.country;
var description= weatherData.weather[0].description;
var temp= weatherData.main.temp;
$('.weatherResponse').append("The Weather in " + cityName + " " + country + " is currently " + description + " with temperature: " + temp);
}
}
</script>
</body>
您可以将您的城市名称和div(显示结果)作为函数参数传递
function getWeather(cityName, className)
{
$('.'+className+'').html('');
var apiCall= 'http://api.openweathermap.org/data/2.5/weather?q=' + cityName + '&mode=json&units=metric&appid=d3d913fca612366a61837ea39a622480';
$.getJSON(apiCall,weatherCallback);
function weatherCallback(weatherData)
{
var cityName= weatherData.name;;
var country= weatherData.sys.country;
var description= weatherData.weather[0].description;
var temp= weatherData.main.temp;
$('.'+className+'').append("The Weather in " + cityName + " " + country + " is currently " + description + " with temperature: " + temp);
}
}
<script src="https://code.jquery.com/jquery-3.0.0.js"></script>
<body>
<button onclick="getWeather('dehradun','wr1')">Get weather of Dehradun</button>
<br><br>
<div class="wr1"></div>
<br><br>
<button onclick="getWeather('pune','wr2')">Get weather of Pune</button>
<br><br>
<div class="wr2"></div>
<br>
</body>
我在我的网站上使用来自 OpenWeatherMap 的 API 来仅在一个页面中显示某些特定地点的天气状况。 这是代码:
<script src="https://code.jquery.com/jquery-3.0.0.js"></script>
<body>
<input type="hidden" name="name" value="dehradun" id="cityName">
<button onclick="getWeather()">Get weather of Dehradun</button>
<div class="weatherResponse"></div>
<br><br>
<input type="hidden" name="name" value="pune" id="cityName">
<button onclick="getWeather()">Get weather of Pune</button> '
<div class="weatherResponse"></div>
<br><br>
<script>
function getWeather() {
$('.weatherResponse').html('');
var cityName = $('#cityName').val();
var apiCall = 'http://api.openweathermap.org/data/2.5/weather?q=' + cityName + '&mode=json&units=metric&appid=<apikey>';
$.getJSON(apiCall, weatherCallback);
function weatherCallback(weatherData) {
var cityName = weatherData.name;;
var country = weatherData.sys.country;
var description = weatherData.weather[0].description;
var temp = weatherData.main.temp;
$('.weatherResponse').append("The Weather in " + cityName + " " + country + " is currently " + description + " with temperature: " + temp);
}
}
</script>
</body>
假设如果我想使用相同的 Javascript 函数显示两个地方的天气状况,那么它会为两个地方(代码中首先提到的地点(台拉登))显示相同的结果。
帮我解决这个问题。
我已经大致添加了下面的工作代码,当你点击一个按钮时,它会在两个文本中显示该城市的是否报告,尝试点击两个按钮看看:
<script src="https://code.jquery.com/jquery-3.0.0.js"></script>
<body>
<input type="hidden" name="name" value="dehradun" id="cityName2">
<button onclick="getWeather('dehradun')">Get weather of Dehradun</button>
<div class="weatherResponse"></div>
<br><br>
<input type="hidden" name="name" value="pune" id="cityName1">
<button onclick="getWeather('pune')">Get weather of Pune</button>
<br><br>
<div class="weatherResponse"></div>
<script>
function getWeather(cityName){
$('.weatherResponse').html('');
var apiCall= 'http://api.openweathermap.org/data/2.5/weather?q=' + cityName + '&mode=json&units=metric&appid=d3d913fca612366a61837ea39a622480';
$.getJSON(apiCall,weatherCallback);
function weatherCallback(weatherData){
var cityName= weatherData.name;;
var country= weatherData.sys.country;
var description= weatherData.weather[0].description;
var temp= weatherData.main.temp;
$('.weatherResponse').append("The Weather in " + cityName + " " + country + " is currently " + description + " with temperature: " + temp);
}
}
</script>
</body>
您可以将您的城市名称和div(显示结果)作为函数参数传递
function getWeather(cityName, className)
{
$('.'+className+'').html('');
var apiCall= 'http://api.openweathermap.org/data/2.5/weather?q=' + cityName + '&mode=json&units=metric&appid=d3d913fca612366a61837ea39a622480';
$.getJSON(apiCall,weatherCallback);
function weatherCallback(weatherData)
{
var cityName= weatherData.name;;
var country= weatherData.sys.country;
var description= weatherData.weather[0].description;
var temp= weatherData.main.temp;
$('.'+className+'').append("The Weather in " + cityName + " " + country + " is currently " + description + " with temperature: " + temp);
}
}
<script src="https://code.jquery.com/jquery-3.0.0.js"></script>
<body>
<button onclick="getWeather('dehradun','wr1')">Get weather of Dehradun</button>
<br><br>
<div class="wr1"></div>
<br><br>
<button onclick="getWeather('pune','wr2')">Get weather of Pune</button>
<br><br>
<div class="wr2"></div>
<br>
</body>