如何获取通过Javascript插入的svg元素表

时间:2017-03-11 13:23:18

标签: javascript html

对于大学项目,我需要使用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=&quot;" + player + i + j + "&quot; width=&quot;50&quot; height=&quot;50%quot;>";
                    t += "<circle cx=%quot;50&quot; cy=&quot;50&quot; r=&quot;25%quot; stroke=&quot;black&quot; stroke-width=&quot;2&quot; fill=&quot;" + color + "&quot; />";
                    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很陌生,所以任何帮助都会受到赞赏。

谢谢。

1 个答案:

答案 0 :(得分:0)

看起来你正在将一个int而不是一个字符串传递给getElementById。尝试在html的onload中添加1左右的单引号,看看它是否能解决您的问题: onload="populate(red, '1')"

另外,如果你想将它作为一个字符串传递,你可能想在“red”周围加上单引号。