<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title>符合WEB标准的文字间隔滚动JS代码</title> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
</head> 
<body> 
<script type="text/javascript">
var marqueeContent=new Array();
marqueeContent[0]='小泉称若自民党在议会选举中失败';
marqueeContent[1]='布什发表广播讲话';
marqueeContent[2]='伊斯兰武装炸毁印控克什米尔铁路导致列车出轨';
marqueeContent[3]='布雷默:即使抓住了萨达姆也难以结束抵抗行动';
var marqueeInterval=new Array();  //定义一些常用而且要经常用到的变量
var marqueeId=0;
var marqueeDelay=3000;  //更新时间,毫秒
var marqueeHeight=20;  //容器高度function initMarquee() {
 var str=marqueeContent[0];
 document.write('<div id="marqueeBox" style="overflow:hidden;height:'+marqueeHeight+'px" onmouseover="clearInterval(marqueeInterval[0])" onmouseout="marqueeInterval[0]=setInterval(\'startMarquee()\',marqueeDelay)"><div>'+str+'</div></div>');
 marqueeId++;
 marqueeInterval[0]=setInterval("startMarquee()",marqueeDelay);
 }
function startMarquee() {
 var str=marqueeContent[marqueeId];
  marqueeId++;
 if(marqueeId>=marqueeContent.length) marqueeId=0;
 if(document.getElementById("marqueeBox").childNodes.length==1) {
  var nextLine=document.createElement('DIV');
  nextLine.innerHTML=str;
  document.getElementById("marqueeBox").appendChild(nextLine);
  }
 else {
  document.getElementById("marqueeBox").childNodes[0].innerHTML=str;
  document.getElementById("marqueeBox").appendChild(document.getElementById("marqueeBox").childNodes[0]);
  document.getElementById("marqueeBox").scrollTop=0;
  }
 clearInterval(marqueeInterval[1]);
 marqueeInterval[1]=setInterval("scrollMarquee()",20);
 }
function scrollMarquee() {
 document.getElementById("marqueeBox").scrollTop++;
 if(document.getElementById("marqueeBox").scrollTop%marqueeHeight==(marqueeHeight-1)){
  clearInterval(marqueeInterval[1]);
  }
 }
initMarquee();
</script>
</body> 
</html>w3c 验证返回的错误信息是:
Line 21, Column 214: document type does not allow element "div" here 
…al(\'startMarquee()\',marqueeDelay)"><div>'+str+'</div></div>');

解决方案 »

  1.   

    The element named above was found in a context where it is not allowed. This could mean that you have incorrectly nested elements -- such as a "style" element in the "body" section instead of inside "head" -- or two elements that overlap (which is not allowed). One common cause for this error is the use of XHTML syntax in HTML documents. Due to HTML's rules of implicitly closed elements, this error can create cascading effects. For instance, using XHTML's "self-closing" tags for "meta" and "link" in the "head" section of a HTML document may cause the parser to infer the end of the "head" section and the beginning of the "body" section (where "link" and "meta" are not allowed; hence the reported error). 看下面的说明好像是说div不能嵌套使用。
    另外也可能是在body中使用了应该放在head里面的元素,不过这里貌似没有。[color=#0000FF]这个是样式么?没见过这种写法,个人认为这里有点诡异
      

  2.   

    校验器把document.write里面的内容当成html标签了。
    把那个document.write改成document.createElement
    或者这样:
    var string = "";
    string += '<';
    string += 'div id="marqueeBox" ';
    string += 'style="overflow:hidden;height:'+marqueeHeight+'px" ';
    string += 'onmouseover="clearInterval(marqueeInterval[0])" ';
    string += 'onmouseout="marqueeInterval[0]=setInterval(\'startMarquee()\',marqueeDelay)">';
    string += '<';
    string += 'div>'+str+'<'+'/div><'+'/div>';
    document.write(string);
      

  3.   

    IE7,firefox3.5都没问题<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
    <html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
    <title>符合WEB标准的文字间隔滚动JS代码</title> 
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
    </head> 
    <body > 
    <script type="text/javascript">
    var marqueeContent=[];
    marqueeContent[0]='小泉称若自民党在议会选举中失败';
    marqueeContent[1]='布什发表广播讲话';
    marqueeContent[2]='伊斯兰武装炸毁印控克什米尔铁路导致列车出轨';
    marqueeContent[3]='布雷默:即使抓住了萨达姆也难以结束抵抗行动';
    var marqueeInterval=[];  //定义一些常用而且要经常用到的变量
    var marqueeId=0;
    var marqueeDelay=1000;  //更新时间,毫秒
    var marqueeHeight=20;  //容器高度function initMarquee() {
     var str=marqueeContent[0];
     document.write('<div id="marqueeBox" style="overflow:hidden;height:'+marqueeHeight+'px" onmouseover="clearInterval(marqueeInterval[0])" onmouseout="marqueeInterval[0]=setInterval(\'startMarquee()\',marqueeDelay)"><div>'+str+'</div></div>');
     marqueeId++;
     marqueeInterval[0]=setInterval("startMarquee()",marqueeDelay);
     }function startMarquee() {
     var str=marqueeContent[marqueeId];
     marqueeId++;
     if(marqueeId>=marqueeContent.length) marqueeId=0;
     if(document.getElementById("marqueeBox").childNodes.length==1) {
      var nextLine=document.createElement('DIV');
      nextLine.innerHTML=str;
      document.getElementById("marqueeBox").appendChild(nextLine);
      }
      else {
      document.getElementById("marqueeBox").childNodes[0].innerHTML=str;
      document.getElementById("marqueeBox").appendChild(document.getElementById("marqueeBox").childNodes[0]);
      document.getElementById("marqueeBox").scrollTop=0;
      }
      clearInterval(marqueeInterval[1]);
      marqueeInterval[1]=setInterval("scrollMarquee()",20);
     }function scrollMarquee() {
    document.getElementById("marqueeBox").scrollTop++;
        if(document.getElementById("marqueeBox").scrollTop%marqueeHeight==(marqueeHeight-1)){
    clearInterval(marqueeInterval[1]);
        }
     }initMarquee();
    </script>
    </body > </html>
      

  4.   

    这段代码本身就可以运行,问题是无法通过 w3c xhtml strict 标准。楼上几位老师的代码依然无法通过 w3c validator测试。
      

  5.   

    能在IE,FF下运行,就可以了。大众用的最多的就这两种,LZ的需求貌似太过鸡蛋挑骨头~