通过jQuery用html文本写入json数据的示例 [英] example to write json data by jquery with html text

查看:360
本文介绍了通过jQuery用html文本写入json数据的示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编程的初学者,我希望有人给我一个例子,向我展示如何用HTML文本写入JSON数据

I am beginner in programming, I want someone to give me an example to show me how to write JSON data with HTML text

<!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中添加以下内容:

You can add in your html something like:

 <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用html文本写入json数据的示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆