可能重复:
JavaScript: How do I print multiple script tags in a loop?
我试图从数组中打印这些元素,但我得到一个空白的屏幕
<div id="playerContainer" style="width: 20em; height: 180px; float: left;">
<object id="player"></object>
</div>
<div id="videos2"></div><script type = "text/javascript">
for (var i=0; i<arr.length; i++) {
document.write("<script
type="text/javascript"
src="http://gdata.youtube.com/feeds/api/videos?q=arr["+i+"] &max-results=8&orderby=relevance&alt=json-in-script&format=5&callback=showMyVideos2">
</script>");
}
</script>
答案 0 :(得分:2)
我的版本 - 假设arr包含您要发送到q =
的内容<div id="playerContainer" style="width: 20em; height: 180px; float: left;">
<object id="player"></object>
</div>
<div id="videos2"></div><script type="text/javascript">
var text ="";
for (var i=0; i<arr.length; i++) {
text += '<script type="text/javascript" '+
'src="http://gdata.youtube.com/feeds/api/videos?q='+arr[i]+
'&max-results=8&orderby=relevance&alt=json-in-script&format=5&callback=showMyVideos2">'+
'<\/script>';
}
document.write(text);
</script>
答案 1 :(得分:0)
<div id="videos2"></div><script type = "text/javascript">
for (var i=0; i<arr.length; i++) {
document.write('<script type="text/javascript" src="http://gdata.youtube.com/feeds/api/videos?q=' + arr[i] + '&max-results=8&orderby=relevance&alt=json-in-script&format=5&callback=showMyVideos2"></scr' + 'ipt>');
}
</script>
答案 2 :(得分:0)
试试这个:
<script type = "text/javascript">
for (var i=0; i<arr.length; i++) {
document.write('< ' + 'script type="text/javascript" src="http://gdata.youtube.com/feeds/api/videos?q='+arr[i]+'&max-results=8&orderby=relevance&alt=json-in-script&format=5&callback=showMyVideos2"><' + '/script>');
}
</script>
你需要转义你的引号,拆分脚本标签,使其不被解析为标签,并在循环中用arr [i]更改你的i变量