美元.getJSON结果为html
$.getJSON results into html
我在这里读了一些例子,但是我仍然有点卡住了。从api中获取提要,格式如下
{
total:123,
id: "1234",
results: [
{
id: "1234",
name: "bobs market",
phone: "123-456-7890",
othervalue: "33333333",
onemore: "get this"
},
{
id: "1235",
name: "jans market",
phone: "123-456-7899",
othervalue: "33333353",
onemore: "get this one"
}
]
}
我正在尝试使用$。getJSON特性循环遍历每个记录,并在id为#bizresults的div中显示每个业务,因此结果html将看起来像这样
<div id="business">
<h4><a href=#>Business Name</a></h4>
<p id="phone">555-555-5555</p>
<p id="adr">
<span id="street-address">12 Main Street</span>
<span id="locality">Sacramento</span>
<span id="region">CA</span>
</div>
我似乎在脚本中得到了结果,我可以在Chrome开发控制台看到它们,但我似乎无法得到输出到我的div。
接受建议,我看了@ Handlebars.js **我仍然没有完全在那里-
<script id="biz_template" type="text/x-handlebars-template">
<div id="businesses">
{{#each jsonResult}}
<a href=#><h4>{{name}}</a></h4>
<p id="phone">{{phone}}</p>
<p id="adr">
<span id="street-address">{{address}}</span>
<span id="locality">{{city}}</span>
<span id="region">{{state}}</span>
</p>
{{/each}}
</div>
</script>
<script>
var source = $("#biz_template").html();
var template = Handlebars.compile(source);
var data = '{"totalCount":61,"impId":"17","jsonResult":[{"impId":"17","listingId":"94523","pageUrl":"/page/LA/new-orleans/ccs-coffee-house/17-94523.html","name":"CC's Coffee House","phone":"(504) 586-0278","address":"650 Poydras St","city":"New Orleans","state":"LA","latitude":"29.949339","longitude":"-90.070017"},{"impId":"17","listingId":"417428","pageUrl":"/page/LA/metairie/ccs-community-coffee-house/17-417428.html","name":"CC's Community Coffee House","phone":"(504) 831-1449","address":"701 Metairie Rd","city":"Metairie","state":"LA","latitude":"29.98763","longitude":"-90.130528"},{"impId":"17","listingId":"228835","pageUrl":"/page/LA/new-orleans/ccs-community-coffee-house/17-228835.html","name":"Cc's Community Coffee House","phone":"(504) 566-1863","address":"228 St Charles Av","city":"New Orleans","state":"LA","latitude":"29.951952","longitude":"-90.069885"}]}';
$("#biz").html(template(data));
</script>
任何想法吗?
我在HTML中有id为biz的div
我刚刚做了一个插件与$.getJSON
和车把,这只是它的一部分,基本上所有你需要的。如果你想的话,我可以把整个插件贴出来。
不要忘记将文件或url引用到句柄
<script src="http://cloud.github.com/downloads/wycats/handlebars.js/handlebars-1.0.rc.1.js" type="text/javascript"></script>
var container = $('#tweets'),
template: $('#tweets-template').html()
$.getJSON(this.url, function(data) {
var tweets = $.map(data.results, function(tweet) {
return {
author: tweet.from_user,
tweet: tweet.text,
thumb: tweet.profile_image_url,
url: 'http://twitter.com/' + tweet.from_user + '/status/' + tweet.id_str
};
});
var templateHandlebars = Handlebars.compile(template);
container.append(templateHandlebars(tweets));
});
//this is the html and the template
<ul id="tweets">
<script id="tweets-template" type="text/x-handlebars-template">
{{#each this}}
<li>
<img src={{thumb}} alt={{author}} />
<p><a href={{url}}>{{tweet}}</a></p>
</li>
{{/each}}
</script>
</ul>
这里的任务是在getJSON成功函数中构建标记。你可以通过连接字符串或使用模板系统(如handlebars、JsRender、underscore等)来实现这一点。如果你正在寻找双向绑定,那就看看Angular或Knockout吧。
相关文章:
- 这是使用html快照和谷歌获取的预期结果吗?SEO/SPA
- AngularJS/HTML/Bootstrap元素用于动态搜索结果
- 使用PHP通过HTML表单选项选择器过滤MYSQL结果
- 计数HTML表中筛选的结果
- 如何将mysql查询的结果获取到html文本框中
- 如何将 JavaScript 中 HTML 的结果写入文件
- 如何等待一个HTML页面并在页面复杂创建时获得结果
- 使用 js 函数显示 HTML 表单的结果
- HTML
“width” 属性在不同的浏览器中产生不同的结果 - 正确显示从 ajax 请求到 html 的结果
- 使用 AJAX 在页面加载时自动提交表单并获得 html 类型结果
- 如何在PHP中包含动态Javascript页面的静态HTML结果
- 在 HTML 文件中获取 PHP 的 HTML 结果
- 解析javascript获取页面标题的html结果
- 如何将URL调用的HTML结果附加到DOM中
- 对象中的jQuery分析HTML结果不支持属性错误
- jQuery解析HTML结果不符合预期
- PHP - cURL post to website ->模拟浏览器(做javascript) ->返回HTML结果
- 如何显示 html 结果形成文本
- 如何获得html()结果的适当子字符串;意想不到的效果