日期:2014-05-16  浏览次数:20317 次

很简单,就是不懂。一个获取函数值的问题
代码如下:
---------------
怎么获取Test中num的值呢?
我不懂js,我原先以为js程序中加入的标签也可以用geElementById等获取
---------------
<html>
<head>
<script>
function Test(){
  var str="";
  str+="Hello,";
  str+="This is a Test!<br />";
  str+="I Love you;<br />";
  str+="I Love you,too!";
var num= Math.random();
  p.innerHTML=str+"<br /><br />"+ "<p id=num>"+num+"</p>";
 
  setTimeout('Test();',1000);
}

</script>
</head>
<body onload=Test();>
<span id="p">12</span>
<input type="button" onClick="Stop();" value="stop" />
</doby>
<script>
function Stop()
{
setTimeout('Test();',10000000);
var num = <!--getElementsById("num")[0].firstChild.data;-->
alert(num);

}
</script>
</html>

------解决方案--------------------
HTML code

<html > 
<head > 
<script > 
function Test(){ 
         var str=""; 
         str+="Hello,"; 
         str+="This is a Test! <br / >"; 
         str+="I Love you; <br / >"; 
         str+="I Love you,too!"; 
         Test.num= Math.random(); 
         document.all.p.innerHTML=str+"<br />"+Test.num; 
} 

</script > 
</head > 
<body onload="Test.timer=window.setInterval('Test()',1000)"> 
<span id="p" >12 </span > 
<input type="button" onClick="Stop();" value="stop" / > 
</doby > 
<script > 
function Stop() 
{ 
    clearInterval(Test.timer);
    alert(Test.num); 
} 
</script > 
</html >

------解决方案--------------------
<html >
<head >
<script >
var timeid;
function Test(){
var str="";
str+="Hello,";
str+="This is a Test! <br / >";
str+="I Love you; <br / >";
str+="I Love you,too!";
  var num= Math.random();
p.innerHTML=str+" <br / > <br / >"+ " <p id=num >"+num+" </p >";
 
timeid = setTimeout( 'Test(); ',1000);
}

</script >
</head >
<body onload=Test(); >
<span id="p" >12 </span >
<input type="button" onClick="Stop();" value="stop" / >
</doby >
<script >
function Stop()
{
clearTimeout(timeid);
var num = document.getElementById("num").innerText;
alert(num);
}
</script >
</html >

是要这个样子的吗?