gpt4 book ai didi

javascript - 单击某个项目时的定位问题

转载 作者:行者123 更新时间:2023-12-01 04:07:12 27 4
gpt4 key购买 nike

我在定位方面遇到问题。我有 5 件元素。它们有自己的最高和最低温度值(以摄氏度和华氏度为单位)。我必须让它们,当我单击单个摄氏图标时,应该只改变它们的值。但是现在,当我单击任何摄氏图标时,它会对所有值进行更改,并且它们会采用第一个项目的值。您可以在这里看到它:

var api_key = "dd39280551e44f1bb34221739171701&q=";
var loc;

$(document).ready(function() {
// var latitude,longitude;


$.getJSON('http://ipinfo.io', function(d){
loc = d.loc.split(",");

var apiLink = "http://api.apixu.com/v1/forecast.json?key=";
var days5 = "&days=5";
var api = apiLink + api_key + loc[0] +','+ loc[1] + days5;


$.getJSON(api, function(v1) {


var tempSwapForecast = true;


var forecastdayData = v1.forecast.forecastday;

var days = forecastdayData.map(function(d) {
return d.day;
});

var maxtempc = days.map(function(maxc) {
return maxc.maxtemp_c;
});

var mintempc = days.map(function(minc) {
return minc.mintemp_c;
});

var maxtempf = days.map(function(maxf) {
return maxf.maxtemp_f;
});

var mintempf = days.map(function(minf) {
return minf.mintemp_f;
});


$('.forecasticons img').each(function(i) {
$(this).attr('src', days[i].condition.icon);
});

$('.frc-temp').each(function(i) {
$(this).html(Math.round(maxtempc[i]) + "-" + Math.round(mintempc[i]));
$('.frc-degree').click(function() {
for (var j = 0; j < $(this).length; j++) {
if(tempSwapForecast===false){
$('.frc-temp').html(Math.round(maxtempc[j]) + "-" + Math.round(mintempc[j]));
$('.frc-degree').html(" &#8451;");
tempSwapForecast = true;
} else {
$('.frc-temp').html(Math.round(maxtempf[j]) + "-" + Math.round(mintempf[j]));
$('.frc-degree').html("&#8457;");
tempSwapForecast = false;
}

}


});



});


$('.forecastText').each(function(i) {
$(this).html(days[i].condition.text);
});


});
});
});
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<div id="other-days">
<div class="forecastday">
<div class="forecasticons" >
<img class="fcicon" alt="weather-icon" src="">
</div>
<div class="forecastText"></div>
<div class="forecasttemp">
<span class="frc-temp"></span>
<a class="frc-degree" href="#">&#8451;</a>
</div>
</div>

<div class="forecastday">
<div class="forecasticons" >
<img class="fcicon" alt="weather-icon" src="">
</div>
<div class="forecastText"></div>
<div class="forecasttemp">
<span class="frc-temp"></span>
<a class="frc-degree" href="#">&#8451;</a>
</div>
</div>

<div class="forecastday">
<div class="forecasticons" >
<img class="fcicon" alt="weather-icon" src="">
</div>
<div class="forecastText"></div>
<div class="forecasttemp">
<span class="frc-temp"></span>
<a class="frc-degree" href="#">&#8451;</a>
</div>
</div>

<div class="forecastday">
<div class="forecasticons" >
<img class="fcicon" alt="weather-icon" src="">
</div>
<div class="forecastText"></div>
<div class="forecasttemp">
<span class="frc-temp"></span>
<a class="frc-degree" href="#">&#8451;</a>
</div>
</div>

<div class="forecastday">
<div class="forecasticons" >
<img class="fcicon" alt="weather-icon" src="">
</div>
<div class="forecastText"></div>
<div class="forecasttemp">
<span class="frc-temp"></span>
<a class="frc-degree" href="#">&#8451;</a>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-2.2.4.js"></script>

</body>
</html>

最佳答案

使其工作的非常小的更改:在您的单击处理程序中,我不是按类查找要填充的字段,而是使用单击的元素查找其父级,然后查找该父级中的类。所以它变成了 $(this).parent().find(...) 所有这些更改都在第 53 行和第 59 行之间。

var api_key = "dd39280551e44f1bb34221739171701&q=";
var loc;

$(document).ready(function() {
// var latitude,longitude;


$.getJSON('http://ipinfo.io', function(d) {
loc = d.loc.split(",");

var apiLink = "http://api.apixu.com/v1/forecast.json?key=";
var days5 = "&days=5";
var api = apiLink + api_key + loc[0] + ',' + loc[1] + days5;


$.getJSON(api, function(v1) {


var tempSwapForecast = true;


var forecastdayData = v1.forecast.forecastday;

var days = forecastdayData.map(function(d) {
return d.day;
});

var maxtempc = days.map(function(maxc) {
return maxc.maxtemp_c;
});

var mintempc = days.map(function(minc) {
return minc.mintemp_c;
});

var maxtempf = days.map(function(maxf) {
return maxf.maxtemp_f;
});

var mintempf = days.map(function(minf) {
return minf.mintemp_f;
});


$('.forecasticons img').each(function(i) {
$(this).attr('src', days[i].condition.icon);
});

$('.frc-temp').each(function(i) {
// Added a 'units' property, so the temperature
// can track what type of unit it is displaying.
$(this).data("units", "c");
$(this).html(Math.round(maxtempc[i]) + "-" + Math.round(mintempc[i]));
});
$('.frc-degree').on("click", function() {
// As we use the .frc-temp el often, reference it once.
var myTempEl = $(this).parent().find(".frc-temp");
// This is the unique index of the clicked day.
var myIndex = $(".forecastday").index(
$(this).parents(".forecastday")
);
/****
* Above, we created a data attribute on the
* .frc-temp element to store the units. By
* doing this, the element becomes self-
* contained. Here, we can toggle the units
* based on that data attribute.
****/
if (myTempEl.data("units") === "f") {
// First, switch the unit attribute...
myTempEl.data("units", "c");
// Then, replace the contents of the temp el
myTempEl.html(
Math.round(maxtempc[myIndex]) +
"-" +
Math.round(mintempc[myIndex]));
// Then, set the contents of this to 'c'
$(this).html(" &#8451;");
tempSwapForecast = true;
} else {
myTempEl.data("units", "f");
myTempEl.html(
Math.round(maxtempf[myIndex]) +
"-" +
Math.round(mintempf[myIndex]));
$(this).html("&#8457;");
tempSwapForecast = false;
}

});



$('.forecastText').each(function(i) {
$(this).html(days[i].condition.text);
});


});
});
});
<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>

<body>
<div id="other-days">
<div class="forecastday">
<div class="forecasticons">
<img class="fcicon" alt="weather-icon" src="">
</div>
<div class="forecastText"></div>
<div class="forecasttemp">
<span class="frc-temp"></span>
<a class="frc-degree" href="#">&#8451;</a>
</div>
</div>

<div class="forecastday">
<div class="forecasticons">
<img class="fcicon" alt="weather-icon" src="">
</div>
<div class="forecastText"></div>
<div class="forecasttemp">
<span class="frc-temp"></span>
<a class="frc-degree" href="#">&#8451;</a>
</div>
</div>

<div class="forecastday">
<div class="forecasticons">
<img class="fcicon" alt="weather-icon" src="">
</div>
<div class="forecastText"></div>
<div class="forecasttemp">
<span class="frc-temp"></span>
<a class="frc-degree" href="#">&#8451;</a>
</div>
</div>

<div class="forecastday">
<div class="forecasticons">
<img class="fcicon" alt="weather-icon" src="">
</div>
<div class="forecastText"></div>
<div class="forecasttemp">
<span class="frc-temp"></span>
<a class="frc-degree" href="#">&#8451;</a>
</div>
</div>

<div class="forecastday">
<div class="forecasticons">
<img class="fcicon" alt="weather-icon" src="">
</div>
<div class="forecastText"></div>
<div class="forecasttemp">
<span class="frc-temp"></span>
<a class="frc-degree" href="#">&#8451;</a>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-2.2.4.js"></script>

</body>

</html>
这里有很多小变化:frc-temp 元素现在有一个数据属性,用于跟踪单位是华氏度还是摄氏度,并且我引用元素的方式也略有改变。

关于javascript - 单击某个项目时的定位问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41727295/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com