学习javascript,下面这个代码提示<body onload="startScroll()">这行缺少对象.
还有就是为什么if((num_tips>0)&&(tips[num_tips-1]==""))有这个判断?第一个条件好理解,数组的长度大于0,但是为什么要判断数组的值为空呢?路过的兄弟姐妹知道的能解答下吗?谢谢!!
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<script type="text/javscript">
<!--
var tips=new Array("hello!where go?","it is a wrong","verrrrrrrry good","test");
var num_tips=tips.length;
var index=0;
if((num_tips>0)&&(tips[num_tips-1]==""))
{
--num_tips;
}
function startScroll()
{
if(tips!=null)
{
if(window.document.the_form)
{
window.document.the_form.the_txt.value=tips[index++];
}
if(index>num_tips-1)
{
index=0;
}
}
setTimeout("startScroll()",3500)
}-->
</script>
</HEAD> <BODY onload="startScroll()"> <form name="the_form">
<textarea name="the_txt" rows="3" cols="40"></textarea>
</form>
</BODY>
</HTML>
还有就是为什么if((num_tips>0)&&(tips[num_tips-1]==""))有这个判断?第一个条件好理解,数组的长度大于0,但是为什么要判断数组的值为空呢?路过的兄弟姐妹知道的能解答下吗?谢谢!!
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<script type="text/javscript">
<!--
var tips=new Array("hello!where go?","it is a wrong","verrrrrrrry good","test");
var num_tips=tips.length;
var index=0;
if((num_tips>0)&&(tips[num_tips-1]==""))
{
--num_tips;
}
function startScroll()
{
if(tips!=null)
{
if(window.document.the_form)
{
window.document.the_form.the_txt.value=tips[index++];
}
if(index>num_tips-1)
{
index=0;
}
}
setTimeout("startScroll()",3500)
}-->
</script>
</HEAD> <BODY onload="startScroll()"> <form name="the_form">
<textarea name="the_txt" rows="3" cols="40"></textarea>
</form>
</BODY>
</HTML>
2. tips[num_tips-1]=="" 用于数组最后一个元素是""的话,就取到最后一个元素之前为止,不过,在此没什么意义。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<script type="text/javascript">
<!--
var tips=new Array("hello!where go?","it is a wrong","verrrrrrrry good","test");
var num_tips = tips.length;
var index = 0;
if((num_tips>0)&&(tips[num_tips-1]=="")){
--num_tips;
}
var tid = null;function startScroll()
{
if(tid !=null) {clearTimeout(tid);tid=null}
if(tips!=null)
{
if(document.the_form)
{
document.the_form.the_txt.value=tips[index++];
}
if(index>num_tips-1)
{
index=0;
}
}
tid = setTimeout("startScroll()",3500)
}
-->
</script>
</HEAD><BODY onload="startScroll()">
<form name="the_form">
<textarea name="the_txt" rows="3" cols="40"></textarea>
</form>
</BODY>
</HTML>
应该是<script type="text/javascript">
你写错了,整个js就没被执行,自然不认识startScroll()函数。
不清楚这段代码是出于什么逻辑考虑的,从这个代码中分析,这个代码是不会被执行的。
if((num_tips>0)&&(tips[num_tips-1]==""))
{
--num_tips;
}
tips[num_tips-1]==""来判断空值,非常感谢2位。哈哈。