示例: - jsFiddle
此脚本:
var slides = [];
$("ul .image").each(function() {
var this$ = $(this);
var obj = {};
obj.image = this$.text();
slides.push(obj);
});
document.body.innerHTML = JSON.stringify(slides);
制作了这个HTML:
<ul>
<li><span class="image">/images/image1.jpg</span></li>
<li><span class="image">/images/image2.jpg</span></li>
</ul>
此输出:
[{"image":"/images/image1.jpg"},{"image":"/images/image2.jpg"}]
如何摆脱 “图片”周围的惊叹号: 在冒号前的每个开头?
谢谢你,琳达:)))
编辑:
预期结果如下:
[
{image:"/images/image1.jpg"},
{image:"/images/image2.jpg"}
]
答案 0 :(得分:0)
试试这个:jsfiddle
var slides = [];
$("ul .image").each(function() {
var this$ = $(this);
var obj = {};
obj.image = this$.text();
obj.title = this$.nextAll(".title").text();
obj.thumb = this$.nextAll(".thumb").text();
slides.push(obj);
});
var str = JSON.stringify(slides);
document.body.innerHTML = str.replace(/"image"/g , "image");