简体   繁体   English

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

[英]How to get a Table of svg elements inserted via Javascript

For a college project I need to make a 4-in-a-row game using html, javascript and bootstrap, so far it's been going well but I can't seem to get the creation of my svg elements correct. 对于大学项目,我需要使用html,javascript和bootstrap制作一个四排的游戏,到目前为止它一直很顺利,但我似乎无法正确创建我的svg元素。 I'm using the following code to create the svg elements: 我正在使用以下代码来创建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>

And this is an extract from my HTML: 这是我的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>

I'm quite new to HTML so any help would be appreciated. 我对HTML很新,所以任何帮助都会受到赞赏。

Thank you. 谢谢。

It looks like you are passing an int instead of a string to getElementById . 看起来你正在将一个int而不是一个字符串传递给getElementById Try to add single quotes around 1 in the onload in your html and see if it fixes your problem: onload="populate(red, '1')" 尝试在html的onload中添加1左右的单引号,看看它是否修复了你的问题: onload="populate(red, '1')"

Also, you might want to put single quotes around 'red' if you want to pass that as a string as well. 此外,如果您想将其作为字符串传递,则可能需要在“红色”周围添加单引号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM