我是编程初学者,我希望有人能给我一个例子,告诉我如何用HTML
文本编写JSON
数据
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
</head>
<body>
<h1>antar<h2>
<script>
$.getJSON('http://api.wipmania.com/jsonp?callback=?', function (data) {
document.open();
document.write('Latitude: ' + data.latitude + '\nLongitude: ' + data.longitude + '\nCountry: ' + data.address.country);
document.close();
});
</script>
</body>
</html>
最佳答案
您可以在您的 html 中添加如下内容:
<h2 id='lat'></h2>
<h2 id='long'></h2>
<h2 id='country'></h2>
然后将您的脚本重构为:
$(document).ready(function(){
$.getJSON('http://api.wipmania.com/jsonp?callback=?', function (data) {
$("#lat").text(data.latitude);
$("#long").text(data.longitude);
$("#country").text(data.country);
});
});
关于jquery - 用 jquery 用 html 文本写 json 数据的例子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14100323/