Wie auf CSS zugreifen ohne onload

one6666

Mitglied Titanium
Hallo,

ich verstehe gerade die Welt nicht mehr, man ist das assozial:(

Das geht:
HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Steuerung</title>
<style type="text/css">
.box{
	background:#CF0;
	width:50px;
	height:50px;
	position:relative;
}
</style>
<script type="text/javascript">

function bewegen(i){
  if(typeof i == "undefined"){
  i = 0;
  }

  if(i < 1000){
  i++;
  document.getElementById("b_box").style.left=i+"px";
  window.setTimeout("bewegen("+i+")", 10);
  }
}

</script>
</head>

<body onload="bewegen()">
<div id="b_box" class="box"></div>
</body>
</html>

Aber das nicht:
HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Steuerung</title>
<style type="text/css">
.box{
	background:#CF0;
	width:50px;
	height:50px;
	position:relative;
}
</style>
<script type="text/javascript">

function bewegen(i){
  if(typeof i == "undefined"){
  i = 0;
  }

  if(i < 1000){
  i++;
  document.getElementById("b_box").style.left=i+"px";
  window.setTimeout("bewegen("+i+")", 10);
  }
}
document.onkeypress = bewegen;
</script>
</head>

<body>
<div id="b_box" class="box"></div>
</body>
</html>

Dabei habe ich genaud as gleiche Gestern gemacht und da geht es o_O

Weiß jemand warum das so ist und wie ich das umgehen kann?
 
Javascript:
if(typeof i != "number"){
    i = 0;
}

Weil i ist nicht "undefined", wenn du nichts übergibts, sondern onkeypress übergibt dort infos über die gedrückte Taste, also typeof i ist "object".
 

Neue Beiträge

Zurück