我是编程的初学者,我希望有人给我一个例子来告诉我如何JSON用HTML文本写数据
<!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>
Run Code Online (Sandbox Code Playgroud)
您可以在html中添加如下内容:
<h2 id='lat'></h2>
<h2 id='long'></h2>
<h2 id='country'></h2>
Run Code Online (Sandbox Code Playgroud)
然后重构您的脚本:
$(document).ready(function(){
$.getJSON('http://api.wipmania.com/jsonp?callback=?', function (data) {
$("#lat").text(data.latitude);
$("#long").text(data.longitude);
$("#country").text(data.country);
});
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10173 次 |
| 最近记录: |