用来自JSON调用的内容替换div [英] Replace divs with content from JSON call

查看:70
本文介绍了用来自JSON调用的内容替换div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从JSON调用接收内容,我想填充3个对象,返回的响应在 news_item 类中,如下所示:

I am receiving content from a JSON call and I would like to populate 3 object that returned with response in news_item class as follows:

<div class="news"> 
        <div class="news_item" style="margin-left: 0 !important;">

        <div class="ni_image">
            <a href="/HaberDetay/TITLE/ID"><img src="IMAGE"width="203" height="109"/></a>
        </div>
        <div class="ni_text">
            <div class="ni_text_title">TITLE</div>
            <div class="ni_text_content"> 
                CONTENT
            </div>
        </div>
        </div> 
</div>

我可以按如下方式接收数据。但不太清楚如何像上面那样填充 news_item div。我还需要清除 news ,并用JSON响应中的数据替换所有内容。

I am able to receive the data as follows. but not quite sure how to populate the news_item div as above. I also need to clear, news and replace everything with data from JSON response.

var newsByCity = JSON.parse(msg);

                $.each(newsByCity, function (key, item) {
                    alert("Code: " + item.City + " ,Name: " + item.Title + " ,Country: " + item.County);

                });

任何指针?我怎么能这样做?

any pointers? how can i do this?

编辑:即时通讯回到标题,ID,内容等我只需要看看我可以创建这个新的divs。

i m getting back TITLE, ID, CONTENT etc. I just need to see how i can create this new divs.

推荐答案

请注意,jQuery模板仍处于测试阶段(可以是DL: https://github.com/jquery/jquery-tmpl

Mind you jQuery templating is still in beta (can be DL here: https://github.com/jquery/jquery-tmpl)

创建这个HTML的jQuery模板,标题/ ID /内容等,他们将出现 $ {TITLE} / etc。基本上这样:

Create a jQuery template of this HTML, with the Title/ID/Content etc where they will be appearing ${TITLE}/etc. Basically like this:

<div class="news_item" style="margin-left: 0 !important;">
    <div class="ni_image">
        <a href="/HaberDetay/${TITLE}/${ID}"><img src="IMAGE"width="203" height="109"/></a>
    </div>
    <div class="ni_text">
        <div class="ni_text_title">${TITLE}</div>
        <div class="ni_text_content"> 
            ${CONTENT}
        </div>
    </div>
</div> 

然后只需创建模板并将其附加到新闻中即可。

Then just create the template and append it to news.

$('#yourTemplateScriptArea')。tmpl(newsByCity).appendTo('。news');

如果您以前从未做过,请参阅模板的介绍。
http://stephenwalther.com /archive/2010/11/30/an-introduction-to-jquery-templates.aspx

Here's an intro on Templating if you've never done it before. http://stephenwalther.com/archive/2010/11/30/an-introduction-to-jquery-templates.aspx

这篇关于用来自JSON调用的内容替换div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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