我正在尝试创建一些“插件”,例如 facebook like 按钮。但是我的循环只呈现第一个 div。而不是其他div。为什么?
<style>body{background: #ccc;}</style>
<div name="q" data-id="3" data-width="200"></div>
<div name="q" data-id="1" data-width="300"></div>
<div name="q" data-id="1" data-width="400"></div>
<script>
var s = document.getElementsByName("q");
for (var i = 0; i < s.length; i++) {
e = s[i];
w = e.attributes['data-width'].value;
i = e.attributes['data-id'].value;
var o={};
o.iframe = document.createElement('iframe');
o.iframe.setAttribute('src', 'http://resources2.news.com.au/images/2012/09/27/1226482/758034-tardar-sauce-the-cat.jpg');
o.iframe.width = w;
o.iframe.height = w + 100;
o.iframe.border = 0;
o.iframe.setAttribute('style', 'border: 0;overflow: visible;');
e.appendChild(o.iframe);
console.log('id:'+i+' width:'+w);
}
</script>