为json格式化foreach时出现问题,它无法正确打开和关闭标记。它的代码如下:
<script type="text/javascript">
$.get('/blogs.json?limit=3', function(result) {
var posts = '';
for (var i = 0; i < result.posts.length; i++) {
posts += '<h3>';
posts += '<a href="#">'+ result.posts[i].Blog.title +'</a>';
posts += '</h3>';
posts += ''+ result.posts[i].Blog.content +'';
}
document.getElementById('bloglist').innerHTML = posts;
});
</script>
输出被搞砸了,因为它没有正确关闭标签等,例如
<h3><a href="#"></h3><p></p></h3></a> etc...
任何想法为什么会发生......
经过调查,我发现它正在删除结束标签。它看起来像这样
<script type="text/javascript">
$.get('/blogs.json?limit=3', function(result) {
var posts = '';
for (var i = 0; i < result.posts.length; i++) {
posts += '<h3>';
posts += '<a href="#">'+ result.posts[i].Blog.title +'';
posts += '';
posts += '<p>'+ result.posts[i].Blog.content +'';
}
document.getElementById('bloglist').innerHTML = posts;
});
</script>