简体   繁体   English

使用模板库从ajax请求接收json数据后生成html代码的最佳方法是什么?

[英]What is the best way to generate html code after receiving json data from an ajax request using a template library?

I am doing something like this, but its a bit pain to style. 我正在做这样的事情,但风格有点痛苦。 Is there any other way like using templates? 有没有其他方式使用模板?

   <div id="resultpanel"></div>

    <script>
    function parseData(jsonresults){
        var results = JSON.parse(jsonresults);
        jQuery("#resultpanel").empty();

        for(var i=0;i<results.length;i++){
            var divph = document.createElement("div");
            var img = document.createElement("img");
            var p = document.createElement("p");

            img.src=results[i].logoname;
            p.innerHTML=results[i].perigrafh;
            jQuery(divph).append(img);
            jQuery(divph).append(p);            
            jQuery("#resultpanel").append(divph);   

        }       
     }
    </script>

Thank you 谢谢

您可以查看underscore templates以获得非常简单的内容,或者像mustache.jshandlebars.js这样的内容,以获得更复杂的内容。

You can try it this way. 你可以这样试试。

var html = "<div><img src=" + results[i].logoname + "/><p>" + results[i].perigrafh + "</p></div>"; 
jQuery("#resultpanel").append(html);

This is just an alternative way of doing it. 这只是另一种方法。

Maybe you can also instead of returning a json object, rather generate the html on the server side, return it and append the result. 也许你也可以代替返回一个json对象,而不是在服务器端生成html,返回它并附加结果。

I don't Know You'll only Looking some template engine or below solution can also help you simplfy. 我不知道你只会看一些模板引擎或以下解决方案也可以帮助你simplfy。

 var Main= jQuery("#resultpanel");
for(var i=0;i<results.length;i++){
    var html="";
     html="<div><img src="+results[i].logoname+ "/><p>"+results[i].perigrafh+"</p></div>";
     Main.append(html);
 }

If you query the data in JSON-format it would be easier for you: cf. 如果您以JSON格式查询数据,那么对您来说会更容易:cf。 $.getJSON() http://api.jquery.com/jQuery.getJSON/ So it boils all down to $ .getJSON() http://api.jquery.com/jQuery.getJSON/所以它归结为

$.getJSON(sourceUrl, function(results){
    elems=result.map(function(o){return "<div><img src='"+o.src+"'></img><p>"+o.perigrafh+"</p></div"});
    $("#resultpanel").html(elems.join(""));
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用JavaScript和GET请求从api网址提取JSON数据的最佳方法是什么? - What is the best way extract JSON data from an api url using javascript and a GET request? 从AJAX附加数据的最佳方法是什么? - What is the best way to append data from AJAX? 收到 JSON 后,将按钮添加到 html 模板 - Add buttons to html template, after receiving JSON 使用Microsoft客户端AJAX库从一个文本框跳到另一个文本框的最佳方法是什么? - What is the best way to jump from one textbox to another using the Microsoft client-side AJAX Library? 使用 JavaScript 动态生成 HTML 元素列表的最佳方法是什么? - What is the best way to generate a list of HTML elements dynamically using JavaScript? 使用 jQuery 重试失败的 AJAX 请求的最佳方法是什么? - What's the best way to retry an AJAX request on failure using jQuery? 使用javascript从json数据简单地生成日期和位置图表的最佳方法? - Best way to simply generate charts through days and locations from json data using javascript? 从ajax请求jQuery接收数据 - receiving data from ajax request jQuery 使用$ .ajax会减慢网站速度吗? 什么是从php文件中检索即时数据的最佳方法 - does using $.ajax slows down the website? what's the best way to retrieve instant data from a php file AngularJS中使用ajax获取数据的最佳方法是什么 - What is the best way in AngularJS to get data using ajax
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM