我正在尝试为Json的div设置属性,但我无法让它工作。手动编写值有效,但在设置时无效。我无法弄清楚我做错了什么。
这是我的代码:
<html>
<head>
<title>ThingSpeak Live Colours</title>
<link rel="stylesheet" href="css/jquery.mobile.structure-1.4.5.min.css" />
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript">
var Temp;
var Humidity;
var Light;
$(document).ready(function () {
$.getJSON('http://api.thingspeak.com/channels/200101/feed/last.json', null, function (data) {
Temp = data["field1"];
Humidity = data["field2"];
Light = data["field3"];
var PreviewGaugeMeter_1 = document.getElementById("PreviewGaugeMeter_1");
var Data_percent_1 = PreviewGaugeMeter_1.getAttribute("data-percent");
PreviewGaugeMeter_1.setAttribute("data-percent", Temp);
});
});
</script>
</head>
<body>
<div class="GaugeMeter" id="PreviewGaugeMeter_1" data-percent="" data-append="%" data-size="90" data-theme="Green-Gold-Red" data-animate_gauge_colors="1" data-animate_text_colors="1" data-width="9" data-label="" data-style="Arch" data-label_color="fff"></div>
</html>
这是我的JSON输出:
{"created_at":"0016-12-12T00:00:00Z","entry_id":37,"field1":" 4","field2":" 7","field3":"4"}
答案 0 :(得分:0)
我找到了一个解决方法,通过获取“$ .getJSON”将变量发送到另一个网页,从那里我使用$ _GET,从网址中检索它们,并且它可以工作。