请问如何用JavaScript split()或其他的方法,将下面的带有分隔符的字段的a1,a2,a3,b1,b2,b3等分别提出,然后输出成html的格式。初始字段:a1|b1|c1|||a2|b2|c2|||a3|b3|c3希望通过用JavaScript,变成如下格式: <a href="/a3">
<img src="/a2"></a>
<div><a href="/a3">a1</a></div>
<a href="/b3">
<img src="/b2"></a>
<div><a href="/b3">b1</a></div>
<a href="/c3">
<img src="/c2"></a>
<div><a href="/c3">c1</a></div>非常感谢,望高手帮助,谢谢了!
2 回答
智慧大石
TA贡献1946条经验 获得超3个赞
var s = "a1|b1|c1|||a2|b2|c2|||a3|b3|c3"; var s1 = s.split("|||"); var hrml = ""; for (var i = 0; i < s1[0].split("|").length; i++) { var a = s1[2].split("|")[i]; var b = s1[1].split("|")[i]; var c = s1[0].split("|")[i]; var temp = "<a href=\"/" + a + "\">\n<img src=\"/" + b + "\"></a>\n<div><a href=\"/" + a + "\">" + c + "</a></div>"; html += temp + "\n\n"; }
Smart猫小萌
TA贡献1911条经验 获得超7个赞
var str = "a1|b1|c1|||a2|b2|c2|||a3|b3|c3"; var arr = str.split("|||"); var html = ""; for (var i = 0; i < arr.length; i++) { arr[i] = arr[i].split("|"); } for (var i = 0; i < arr[0].length; i++) { html+='<a href="/'+arr[2][i]+'">\n<img src="/'+arr[1][i]+'"></a>\n<div><a href="/'+arr[2][i]+'">'+arr[0][i]+'</a></div>\n'; } alert(html); document.write(html);
添加回答
举报
0/150
提交
取消