English 中文(简体)
• 如何将分析结果转换到html
原标题:How to convert ajax response to html

我有网页,用户可以上载自我评论。

装上json评论的法典

$( #profile-comment .stream-more-link ).click(function (e) {
    e.preventDefault();
    //Increment stream page
    streamPage++;
    //Get post data in json
    $.ajax({
        url : "/profile/comments",
        dataType: "json",
        data: {"page":streamPage},
        success: function(response) {
            if (response.length > 0) {
                $( #commentTmpl ).tmpl(response).appendTo( .stream-items );
            } else {
                $( .stream-more ).hide();
            }
        }
    })
    return false;
});

j Query Template

<script type="text/x-jquery-tmpl" id="commentTmpl">
    <div class="stream-item">
        {{if post.length}}
        <div class="article-title">
            <a class="article-title-link" href="${post.titleLink}">${post.title}</a>
        </div>
        {{/if}}
        <div class="comment level-1" id="c${comment.id}">
            <div class="comment-wrap">
                <div class="comment-img">
                    <img src="${user.foto}" width="48px" height="48px" alt="имя профиля" />
                </div>
                <div class="comment-content">
                    <a class="author" href="${user.link}">${user.name}</a>
                    <div class="text">
                        <p>${comment.content}</p>
                    </div>
                    <div class="meta">
                        ${comment.date}
                    </div>
                </div>
            </div>
        </div>
    </div>
</script>

JSON的回复样本来自Chrome json preview:

8: {post:[],…}
comment: {id:46298, content:<i>проверка</i>, на то как<b> </b><b><i>работает</i></b><b> </b>очистка.<br>,…}
content: "<i>проверка</i>, на то как<b> </b><b><i>работает</i></b><b> </b>очистка.<br>"
date: "20.09.2011 02:22"
id: "46298"
post: []
user: {name:moderator, link:/profile/view/username/moderator,…}
foto: "/files/user/fotos/thumb/default.jpg"
link: "/profile/view/username/moderator"
name: "moderator"
9: {post:[], comment:{id:46303,…}, user:{name:moderator, link:/profile/view/username/moderator,…}}
comment: {id:46303,…}
content: "Содержимое с <b>HTML</b>-<i>разметкой <img src="/files/emoticons/love.gif" alt="love" title="love" /><br></i>"
date: "20.09.2011 02:19"
id: "46303"
post: []
user: {name:moderator, link:/profile/view/username/moderator,…}
foto: "/files/user/fotos/thumb/default.jpg"
link: "/profile/view/username/moderator"
name: "moderator"

评论内容没有被评价为html,只是说明。

问题回答

如果我们想要在模板中输出html,我们就必须利用这一顺序{{html varName>。

More about this

http://api.jquery.com/template-tag-html/





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签