可能重複:
How to add onload event to a div?JavaScript的onload沒有工作,但的onclick正在CSS拉繩
我試圖繪製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工作,並且我搞亂了什麼?
的DIV沒有加載事件 – Musa
的「分區的onload」一個簡單的谷歌搜索會給你答案。 –
@musa那麼我怎麼能得到它在該分區創建行onload – kqlambert