对于大学项目,我需要使用html,javascript和bootstrap制作一个4连续的游戏,到目前为止它一直很顺利,但我似乎无法正确创建我的svg元素。我正在使用以下代码来创建svg元素:
<script>
function populate(color, player){
var t = "";
t += "<table>";
for(var i = 0; i < 5; i++)
{
t += "<tr>";
for(var j = 0; j < 5; j++)
{
t += "<td><svg id="" + player + i + j + "" width="50" height="50%quot;>";
t += "<circle cx=%quot;50" cy="50" r="25%quot; stroke="black" stroke-width="2" fill="" + color + "" />";
t += "</svg></td>";
}
t += "</tr>";
}
t += "</table>";
document.getElementById(player).innerHTML = t;
}
</script>
这是我的HTML摘录:
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title"> Player1</h3>
</div>
<div id="1" class="panel-body" onload="populate(red,1)"></div>
<div class="panel-footer">
</div>
</div>
我对HTML很陌生,所以任何帮助都会受到赞赏。
谢谢。
答案 0 :(得分:0)
看起来你正在将一个int而不是一个字符串传递给getElementById
。尝试在html的onload
中添加1左右的单引号,看看它是否能解决您的问题:
onload="populate(red, '1')"
另外,如果你想将它作为一个字符串传递,你可能想在“red”周围加上单引号。