重複の可能性:
onloadイベントをdivに追加する方法は?
ここの例のように、cssとjavascriptで線を引こうとしています:example。元のもので問題なく動作させることができますが、変更してhtmlからonloadで動作させると、onloadは動作しなくなりますが、onclickイベントで動作します。
これが私のCSSです:
.line
{
position: absolute;
height: 0px;
border-width: 2px 0px 0px 0px;
border-style: solid;
border-color: #99aadd;
}
とjavascript:
function createLine2(myline,x1,y1,x2,y2)
{
if (x2 < x1)
{
var temp = x1;
x1 = x2;
x2 = temp;
temp = y1;
y1 = y2;
y2 = temp;
}
var length = Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
myline.style.width = length+"px";
var angle = Math.atan((y2-y1)/(x2-x1));
myline.style.top = y1 + 0.5*length*Math.sin(angle) + "px";
myline.style.left = x1 - 0.5*length*(1 - Math.cos(angle)) + "px";
myline.style.MozTransform = myline.style.WebkitTransform = myline.style.OTransform="rotate("+angle+"rad)";
}
そして最後に、機能しないhtml:
<div class="line" onload="createLine2(this,100,100,0,0)">test</div><br>
そして動作するhtml:
<div class="line" onclick="createLine2(this,100,100,0,0)">test</div><br>
onloadで動作させるにはどうすればよいですか?また、何を台無しにしましたか?