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/

相关文章:

jquery - 过滤jquery中的对象

javascript - 每次在 jquery 中单击按钮时如何使输入添加值?

php - 为什么我不能在这段代码中使用 jQuery 在 HTML 中添加一行?

html - Bootstrap 4 中如何使卡头无边框半径?

json - 为什么marshaling可以序列化循环引用列表而json不可以?

javascript - jQuery tableSorter 中的 dd.mm.yyyy 格式

javascript - 我想保留前 2 个选项并使用 jquery 删除选择元素中的剩余选项,

javascript - 当 AngularJs 中的复选框更改时,向 div 添加和删除类

javascript - json 数据未在 Rails 中传递

json - 从 MVC 5 Controller 返回部分 View 和 JSON