HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<title>doers.lk</title>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<style type="text/css" media="screen">
</style>
</head>
<body>
<p id="hello"></p>
</body>
</html>
JQUERY
$.getJSON('http://api.wipmania.com/jsonp?callback=?', function (data) {
alert('Latitude: ' + data.latitude +
'\n Longitude: ' + data.longitude +
'\n Country: ' + data.address.country);
});
我想将此 json 数据加载到 id="hello"。更好的方法是什么?
最佳答案
jQuery 提供 .html()
让您获取/设置元素内容的方法。在您的回调中,您可以选择其 id 上的元素并使用 .html()
方法添加您的内容。
$.getJSON('http://api.wipmania.com/jsonp?callback=?', function (data) {
$("#hello").html('Latitude: ' + data.latitude +
'<br /> Longitude: ' + data.longitude +
'<br /> Country: ' + data.address.country);
});
关于javascript - 如何加载json数据到id ="hello",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13786701/