【问题标题】:jQuery templates to dynamically render image from a tagjQuery模板从标签动态渲染图像
【发布时间】:2011-09-17 11:30:16
【问题描述】:

在我的模板中我有这个代码:

<ul>
    <li>${members_pics['0']}</li>
    <li>${members_pics['1']}</li>
</ul>

members_pics 属性保存这个值:

<img typeof="foaf:Image" src="http://localhost/parce/sites/default/files/styles/profile_picture/public/pictures/picture-1-1316209044.jpg" alt="" /> 

当脚本处理时,它不会将实际图像呈现在我的页面上,而是将上面的值打印为字符串。

我错过了什么吗?

编辑: 这是我的jQuery代码:

    searchBut.click(function(){
        jQuery.ajax({
           type: "POST",
           url: queryURL,
           data: "country="+countryInput.val(),
           success: function(result){

             searchResults = JSON.parse(result);

             console.log(searchResults);

             jQuery.each(searchResults,function(index){
                this.field_city = this.field_city.und["0"].value;
                 [..]

             });

             jQuery('#parceSearchTpl').tmpl(searchResults).appendTo(searchWarper);  
           }
         });
    })

【问题讨论】:

    标签: jquery jquery-templates


    【解决方案1】:

    您的图片标签正在被 HTML 编码。

    你应该像这样改变你的模板:

    <ul>
        <li>{{html members_pics['0']}}</li>
        <li>{{html members_pics['1']}}</li>
    </ul>
    

    这将输出原始字符串。如果字符串来自不受信任的来源,这可能会使您的网站容易受到 XSS 攻击。

    【讨论】:

    • 感谢您的回答,但尝试后我在 FB 中收到以下错误:missing ) in parenthetical
    【解决方案2】:

    我可以为您的问题想出两种解决方案(如果我理解正确的话)

    • 对您的结果执行new Image(...)
    • 在您的浏览器中测试来自localhost 的图像不太可能奏效。运行本地服务器进行测试

    【讨论】:

    • 那是多么幼稚.. 出于什么原因拒绝了我?对不起,伙计,但你说的没有任何意义
    猜你喜欢
    • 1970-01-01
    • 2020-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-01
    • 1970-01-01
    相关资源
    最近更新 更多