javascript - 生成动态画廊jquery

标签 javascript jquery html asp.net-mvc json

我正在处理一个个人项目,但我在使用 jQuery 生成正确的标记时遇到了麻烦。

我有一个操作可以返回要在此 json 结构中显示的图像列表

[{
    "Id": imageid,
    "imagePath": path to image,
    "imageThumbnailPath": path of the thumbnail,
    "imageTitle": image title,
    "imageSubtitle": image subtitle,
    "CategoryId": id of the category
}]

所以我想动态生成标记以显示不同的图像。我正在使用引导模板,这就是我必须生成的内容:

<div class="span3">
    <div class="picture">
        <a href="path to image" rel="image" title="image title">
            <img src="path to thumbnail" alt="" class="customgalleryclass"  />
        <div class="image-overlay-zoom"></div>
        </a>
    </div>
    <div class="item-description">
        <h5><a href="#">image title</a></h5>
        <p>image subtitle</p>
    </div>
</div>

我正在尝试使用 jQuery append、wrapInner 等生成正确的标记。但由于我不太了解它,所以它产生了困惑。

谢谢!

最佳答案

您不会共享导致困惑的代码。

如果您只是不知道该怎么做,请搜索“jquery template json”。这将是一个好的开始。你可以找到数百种方法来做到这一点。

快速示例我将使用 jquery 和 http://ajax.microsoft.com/ajax/jquery.templates/beta1/jquery.tmpl.min.js

感谢http://www.borismoore.com/2010/10/jquery-templates-is-now-official-jquery.html

您可以在 http://jsfiddle.net/marathonman/EkVvS/ 找到工作示例

HTML

    <div id="myGallery"></div>

模板

    <script id="myGalleryTemplate" type="text/x-jquery-tmpl">
    <div class="span3">
        <div class="picture">
            <a href="${imagePath}" rel="image" title="${imageTitle}">
                <img src="${imageThumbnailPath}" alt="" class="customgalleryclass"  />
            <div class="image-overlay-zoom"></div>
            </a>
        </div>
        <div class="item-description">
            <h5><a href="#">${imageTitle}</a></h5>
            <p>${imageSubtitle}</p>
        </div>
    </div>
    </script>

脚本

    var images = [
    {
        "Id": "imageid",
        "imagePath": "path to image",
        "imageThumbnailPath": "path of the thumbnail",
        "imageTitle": "image title",
        "imageSubtitle": "image subtitle",
        "CategoryId": "id of the category"
    }
    ];

    $( "#myGalleryTemplate" ).tmpl(images).appendTo( "#myGallery" );

关于javascript - 生成动态画廊jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23964777/

相关文章:

javascript - 网站在旧版 Internet Explorer 中无法正常显示

jquery - 使用 jQuery 根据链接文本设置链接 href

html - 如何为 gmail 格式化电子邮件?

javascript - AngularJS 流沙

JavaScript:将输入元素添加到当前 div

javascript - 发布到服务器端代码内的路由

javascript - 通过 onclick 启动的视频不会在 Firefox 中启动,但可以在 Chrome 中启动

javascript - Firefox 中的 "c.defaultView.getComputedStyle() is null"问题

javascript - 使用 split 函数输出不同的值

c# - 如何使用 ASP.NET C# 设置 html 输入类型文本值?