jquery - 用 jquery 用 html 文本写 json 数据的例子

标签 jquery html json

我是编程初学者,我希望有人能给我一个例子,告诉我如何用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);      

});
 });

​Here a working example you can play with .

关于jquery - 用 jquery 用 html 文本写 json 数据的例子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14100323/

相关文章:

javascript 隐藏/显示下拉列表中的项目

html - 将字体规范从 Adob​​e XD 复制到 html/css

javascript - 将几个json传递给javascript

php - Json数组数据在php中插入空值

javascript - Flask,在 cookie 中设置 JSON,并在客户端对其进行解码(在 javascript 中)

javascript - 如何防止子 div 中 anchor 的链接影响主 url?

javascript - JQuery:创建具有数据属性的新元素

html - 是否有任何纯 html 方法将 "combine"标记标记转换为自定义标记?

javascript - 将裁剪后的图像发送到客户端上的 ajax 数据库和服务器上的 PHP

javascript - mvc _layout 在 renderbody() 执行之前调用函数