gpt4 book ai didi

javascript - $.getJSON 结果转为 html

转载 作者:行者123 更新时间:2023-12-02 19:07:37 24 4
gpt4 key购买 nike

我已经在这里阅读了一些示例,但是我仍然有点困惑。我从 api 获取一个像这样格式化的 feed

{
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 和 Handlebars 做了一个插件,这只是它的一部分,基本上是你所需要的。如果您愿意,我可以发布整个插件。

不要忘记引用 Handlebars 的文件或 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>

关于javascript - $.getJSON 结果转为 html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14175293/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com