我想每秒更改sqaure的颜色(#myID:width = height = 100px).(为了检查切换循环是否有效,在每个"case"中我写了console.log("smth happen");.) 但是这个方块的颜色不会改变."小提琴"
接下来,每隔一个document.getElementById('myID')被写入一个新形成的变量thesquare.如何使变量全局,在函数之外?
使用Javascript:
var i = 0;
function changecolor()
{
var thesquare = document.getElementById('myID');
switch (i)
{
case 0 :
thesquare.setAttribute("background-color","red");
++i;
break;
case 1 :
thesquare.setAttribute("background-color","green");
++i;
break;
default :
thesquare.setAttribute("background-color","blue");
i=0;
break;
}
}
setInterval("changecolor()",1000);
Run Code Online (Sandbox Code Playgroud) 我想通过使用JS创建的SVG中的路径绘制一个正方形.但是浏览器不接受这个:
使用Javascript:
var svg = document.createElement('svg');
svg.width = "200";
svg.height = "200";
document.body.appendChild(svg);
var path = document.createElement('path');
path.setAttribute('d','M100,0 L200,100 100,200 0,100Z');
path.setAttribute('fill','red');
svg.appendChild(path);
Run Code Online (Sandbox Code Playgroud)
HTML(输出):
<svg width="200" height="200">
<path d="M100,0 L200,100 100,200 0,100Z" fill="red"/>
</svg>
Run Code Online (Sandbox Code Playgroud)