gpt4 book ai didi

javascript - 如何加载json数据到id ="hello"

转载 作者:行者123 更新时间:2023-11-29 10:49:05 24 4
gpt4 key购买 nike

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);
});

DEMO HERE

我想将此 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);
});​

Working example

关于javascript - 如何加载json数据到id ="hello",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13786701/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com