我正在调用一个返回JSON对象数组的Web服务。我想使用这些对象来填充HTML div。假设每个对象包含一个URL和一个名称。
如果我想为每个对象生成以下HTML:
<div><img src="the url" />the name</div>
在这方面有最佳实践吗?我可以看到几种做法:
- 连接字符串
- 创建元素
- 使用模板插件
- 在服务器上生成HTML,然后通过JSON提供服务。
我正在调用一个返回JSON对象数组的Web服务。我想使用这些对象来填充HTML div。假设每个对象包含一个URL和一个名称。
如果我想为每个对象生成以下HTML:
<div><img src="the url" />the name</div>
在这方面有最佳实践吗?我可以看到几种做法:
var t = $.template('<div><img src="${url}" />${name}</div>');
$(selector).append( t , {
url: jsonObj.url,
name: jsonObj.name
});
我建议你选择更酷、性能更好、易于维护的模板化方法。
\
<h1>${title}</h1>``。 - JoKrvar s="";
for (var i=0; i < 200; ++i) {s += "testing"; }
使用临时数组:
var s=[];
for (var i=0; i < 200; ++i) { s.push("testing"); }
s = s.join("");
使用数组速度更快,特别是在IE浏览器中。我之前用IE7、Opera和FF对字符串进行了一些测试。Opera只需0.4秒就能完成测试,但IE7花了20分钟仍未完成测试(不是开玩笑)。使用数组后,IE浏览器的速度非常快。
前两个选项都是常见且可接受的。
我将在Prototype中给出每个选项的示例。
// assuming JSON looks like this:
// { 'src': 'foo/bar.jpg', 'name': 'Lorem ipsum' }
方法一:
var html = "<div><img src='#{src}' /> #{name}</div>".interpolate(json);
$('container').insert(html); // inserts at bottom
方法二:
var div = new Element('div');
div.insert( new Element('img', { src: json.src }) );
div.insert(" " + json.name);
$('container').insert(div); // inserts at bottom
var tmpl = '<div class="#{classname}">#{content}</div>';
var vals = {
classname : 'my-class',
content : 'This is my content.'
};
var html = $.tmpl(tmpl, vals);
也许更现代的方法是使用像 Mustache这样的模板语言,它在许多语言中都有实现,包括JavaScript。例如:
var view = {
url: "/hello",
name: function () {
return 'Jo' + 'hn';
}
};
var output = Mustache.render('<div><img src="{{url}}" />{{name}}</div>', view);
/* CSS */
.template {display:none;}
<!--HTML-->
<div class="template">
<div class="container">
<h1></h1>
<img src="" alt="" />
</div>
</div>
/*Javascript (using Prototype)*/
var copy = $$(".template .container")[0].cloneNode(true);
myElement.appendChild(copy);
$(copy).select("h1").each(function(e) {/*do stuff to h1*/})
$(copy).select("img").each(function(e) {/*do stuff to img*/})
声明:本人是BOB的维护者。
针对您的具体示例:
<div><img src="the url" />the name</div>
new BOB("div").insert("img",{"src":"the url"}).up().content("the name").toString()
//=> "<div><img src="the url" />the name</div>"
或者使用更短的语法
new BOB("div").i("img",{"src":"the url"}).up().co("the name").s()
//=> "<div><img src="the url" />the name</div>"
这个库非常强大,可以用于创建具有数据插入的非常复杂的结构(类似于d3),例如:
data = [1,2,3,4,5,6,7]
new BOB("div").i("ul#count").do(data).i("li.number").co(BOB.d).up().up().a("a",{"href": "www.google.com"}).s()
//=> "<div><ul id="count"><li class="number">1</li><li class="number">2</li><li class="number">3</li><li class="number">4</li><li class="number">5</li><li class="number">6</li><li class="number">7</li></ul></div><a href="www.google.com"></a>"
目前 BOB 不支持将数据注入到 DOM 中,但这在待办事项中。现在,您可以将输出与普通的 JS 或 jQuery 一起使用,并将其放在任何您想要的位置。
document.getElementById("parent").innerHTML = new BOB("div").insert("img",{"src":"the url"}).up().content("the name").s();
//Or jquery:
$("#parent").append(new BOB("div").insert("img",{"src":"the url"}).up().content("the name").s());
我开发了这个库,因为我不喜欢像jquery和d3这样的替代品。它们的代码非常复杂,很难阅读。在我看来(显然是有偏见的),使用BOB要愉快得多。
BOB可以通过Bower获取,只需运行bower install BOB
即可。
字符串模板
https://www.w3schools.com/js/js_string_templates.asp - Eric