- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是网络开发的新手,我正在尝试弄清楚如何根据 getJSON 的响应显示图像。接收到的数据来自 REST API。然后将数据存储在 2 个数组中,湿度和日期。 Humid 包含字符串形式的湿度。然后将 humid 数组转换为整数数组 - humid_final。我想知道如何根据 humid_final 数组中的特定值在屏幕上显示特定图像。
如果 humid_final 数组中的最新条目 = 50,则显示图像 A。否则显示图像 B。(其中图像 A、B 是本地存储的静态图像)。
$(function updat() {
var url = "https://xpm4zyor39.execute-api.us-west-2.amazonaws.com/prod/entries";
var humid = [],
date = [],
humid_final = []
$.getJSON(url, function (json) {
$(json['Items']).each(function(i, data) {
//Store indicator name
// fill the date array
humid.push(data.humidity);
// fill the string data array
date.push(data.Date);
});
console.log(date);
// query send string that we need to convert into numbers
for (var i = 0; i < humid.length; i++) {
if (humid[i] != null) {
humid_final.push(parseFloat(humid[i]))
} else {
humid_final.push(null)
};
}
var chart = new Highcharts.Chart({
chart: {
type: 'spline',
renderTo: 'container'
},
title: {
text: 'indicatorName'
},
tooltip: {
valueDecimals: 2,
pointFormat: '<span style="color:{point.color}">\u25CF</span> {series.name}: <b>{point.y}%</b><br/>'
},
plotOptions: {
series: {
marker: {
enabled: false
}
}
},
subtitle: {
text: 'Source: World Bank Data'
},
xAxis: {
categories: date //.reverse() to have the min year on the left
},
series: [{
name: 'humidity',
data: humid_final //
}]
});
}); //getJSON method
setTimeout(updat, 3000);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<script src= "opendataAPI.js"></script>
<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
最佳答案
你快到了。为您的 html 中的图像标签指定一个 id,并使其指向标准图像 imageB,例如 <img id="changingImage" src="./images/imageB.png" />
那么让我们尝试翻译这个...
If the latest entry in humid_final array= 50 display image A. Else display image B.
进入javascript:
if (humid_final[humid_final.length-1] == 50) {
document.getElementById('changingImage').src="./images/imageA.png";
}
关于javascript - 根据 getJSON 数据的响应显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45762003/
我正在尝试获取客户端的 IP 地址和 GPS 坐标。使用 jQuery,我有这个: $.getJSON("http://jsonip.appspot.com/", function(data) {
这个问题已经有答案了: Why does JQuery.getJSON() have a success and a done function? (2 个回答) 已关闭 6 年前。 我想知道这两个代
所以首先,是的,我知道有几个答案,但没有一个能够解决我的问题。首先我将展示我的代码HTML: Shop
假设我有以下 JavaScript: (function($) { $.getJSON(url, function(data) { $.each(data.rows, function(i
是否可以使用 jQuery 在另一个 getJSON 请求中使用 getJSON 请求? 像这样: // Population the Requests List // jQuery AJAX cal
我有以下代码,该代码已针对此问题进行了简化。基本上我有一个循环,在每次迭代中调用 jquery getJSON 函数,调用 API 端点来获取一些天气数据。问题是,当 getJSON 请求被触发时,我
我有一个使用 getJSON 的函数,但它没有像我预期的那样工作。 function balbla(name, param) { $.getJSON("/blabla.json?nam
我有一段代码,例如: $.getJSON("http://mysite.org/polls/saveLanguageTest?url=" + escape(window.location.href)
我正在使用jquery.getJSON() ,但我不知道如何进行错误处理。这些是我需要处理的一些情况。 1)如果返回的数据为null怎么办? 2)如果返回的数据不能解析json怎么办? 3) 如果返回
我正在通过参与一个测试项目(包括 SubSonic 和 jQuery)来学习 asp.net mvc。 我遇到的问题是,每次我想要返回的不仅仅是简单字符串(例如 Json 对象)时,我都会遇到困难,因
执行跨域查询,如果运行的URL不可用(404),如何执行某个功能?我尝试这样的事情: $.getJSON({ url:'example.php?callback=?', statusCode: { 4
我在 jQuery 中搜索了相关主题,但没有找到任何方法来解决我的问题。 $(document).ready(function(){ $("#inputForm").submit(functi
当调用 yahoo Web 服务 (http://boss.yahooapis.com/ysearch) 返回数据集时,是否可以设置超时并在超时后退出例程? jQuery.getJSON("http:
我正在使用 jQuery getJSON() 函数。这个函数获取数据没有问题。但有时等待,等待等待......我的加载栏在页面中心显示加载加载。 所以 jQuery ajax() 函数有一个超时变量。
我有一个 html 代码: asd $('button').click( function() { $.getJSON('/schedule/test/', function
目标:我所追求的是每次在数据库中添加某些内容时(在 $.ajax 到 Submit_to_db.php 之后),从数据库获取数据并刷新 main.php(通过 draw_polygon 更明显)。 所
我已经阅读了文档并用谷歌搜索了此内容,但没有看到问题所在。我正在尝试从本地 json 文件获取一些数据。我已在 JSONLint 验证了响应数据 代码 $(document).ready(functi
我在json.getJSON方法上遇到麻烦。这是我当前的代码: var jqxhr = $.getJSON("http://127.0.0.1:5002?callback=?", function()
我需要进行跨域请求,并且 getJSON 有问题。 $.getJSON("http://usr:pwd@10.0.1.xx/cgi-bin/remote/request.cgi?m=json&r=gr
这个问题已经有答案了: How do I return the response from an asynchronous call? (42 个回答) 已关闭 8 年前。 这是我的问题 我有一个充满
我是一名优秀的程序员,十分优秀!