ConfusedOfLife
09-10-2002, 11:48 PM
Look at this code ( you probably need to run it! )
<html>
<head>
<title>Untitled</title>
<style>
div
{
width : 90px;
cursor : default;
}
</style>
<script>
divs = ["1", "2", "3", "4", "kk"];
function showID(which)
{
alert(which);
}
function init()
{
for (i=0; i<divs.length; i++)
document.getElementById(divs[i]).onmouseover = function() { setTimeout("showID(" + this.id + ")",1000); }
}
</script>
</head>
<body onload="init()">
<div id="1">
It's a test!
</div>
<div id="2">
It's a test!
</div>
<div id="3">
It's a test!
</div>
<div id="4">
It's a test!
</div>
<div id="kk">
It's a test!
</div>
</body>
</html>
Move the mouse on the titles, as you can see through the code, by passing each div, you'll see an alert box after one second
showing the id of that div, it's true for all of them, except the last one, that's id is not a number, but instead a string, i.e
"kk", so, what's the reason that it shows "[object]" in the alert box?!
I solved this by re-writing the showID function in this way:
function showID(which)
{
if ( which.id != null )
alert(which.id)
else
alert(which);
}
But I still don't know why it behaves so wierd!
<html>
<head>
<title>Untitled</title>
<style>
div
{
width : 90px;
cursor : default;
}
</style>
<script>
divs = ["1", "2", "3", "4", "kk"];
function showID(which)
{
alert(which);
}
function init()
{
for (i=0; i<divs.length; i++)
document.getElementById(divs[i]).onmouseover = function() { setTimeout("showID(" + this.id + ")",1000); }
}
</script>
</head>
<body onload="init()">
<div id="1">
It's a test!
</div>
<div id="2">
It's a test!
</div>
<div id="3">
It's a test!
</div>
<div id="4">
It's a test!
</div>
<div id="kk">
It's a test!
</div>
</body>
</html>
Move the mouse on the titles, as you can see through the code, by passing each div, you'll see an alert box after one second
showing the id of that div, it's true for all of them, except the last one, that's id is not a number, but instead a string, i.e
"kk", so, what's the reason that it shows "[object]" in the alert box?!
I solved this by re-writing the showID function in this way:
function showID(which)
{
if ( which.id != null )
alert(which.id)
else
alert(which);
}
But I still don't know why it behaves so wierd!