简体   繁体   English

JavaScript验证w3c

[英]Javascript Validation w3c

I'm trying to show/hide content in the following doctype: !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" 我正在尝试显示/隐藏以下doctype中的内容: !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"

Here's my javascript: 这是我的JavaScript:

<script type="text/javascript"> function question(clicked) {
      var q = document.getElementsByTagName("div");
      for(var x=0; x<q.length; x++) {
            title = q[x].getAttribute("title");
            if (title == 'q') {
                  if (q[x].id == clicked) {
                        if (q[x].style.display == 'block') {
                              q[x].style.display = 'none';
                        }
                        else {
                              q[x].style.display = 'block';
                        }
                  }else {
                        q[x].style.display = 'none';
                  }

           }
      } } </script>

On Validation, it returns these errors: 1. Error Line 9, Column 30: character ";" 验证时,它将返回以下错误:1.错误行9,第30列:字符“;” not allowed in attribute specification list 属性规范列表中不允许

        for(var x=0; x<q.length; x++) {
  1. Error Line 9, Column 30: element "q.length" undefined 错误行9,第30栏:元素“ q.length”未定义

      for(var x=0; x<q.length; x++) { 
  2. Error Line 25, Column 9: end tag for "q.length" omitted, but OMITTAG NO was specified 错误行25,第9栏:省略了“ q.length”的结束标记,但指定了OMITTAG NO

  3. Info Line 9, Column 21: start tag was here 信息第9行,第21列:开始标记在此处

      for(var x=0; x<q.length; x++) { 

I'm learning Javascript now, and have tried to Google & fix this about 3 dozen ways by now. 我现在正在学习Javascript,到目前为止,已经尝试使用Google修复这3种方法。 Can anyone help me? 谁能帮我? If I need to try a different script to show/hide, at this point, I'd scrap what I have and do it. 如果需要尝试使用其他脚本来显示/隐藏,在这一点上,我将废弃已有的内容并执行此操作。

Thanks in advance! 提前致谢!

Use CDATA sections. 使用CDATA部分。 The < has a special meaning for the parser. <对于解析器具有特殊含义。

<script>
//<![CDATA[
 ...JavaScript code..
//]]></script>

The validator has detected a < , and attempts to parse a new tag <q.length . 验证器已检测到< ,并尝试解析新标签<q.length When the semicolon is spotted, the parser don't know how to handle it, and throws an error. 发现分号时,解析器不知道如何处理它,并引发错误。 By using CDATA , you effectively say "Anything inside this section should be interpreted as plain-text, and not be parsed. 通过使用CDATA ,您可以有效地说:“本节中的所有内容都应解释为纯文本,而不应进行分析。

Paste your code at http://validator.w3.org/check (validate by direct input), using the following settings: "Validate Document fragment, XHTML 1.0". 使用以下设置将代码粘贴到http://validator.w3.org/check (通过直接输入进行验证):“验证文档片段,XHTML 1.0”。

If you move your javascript functions into an external file, and then link to that file from your page head section, then the validator doesn't have to worry about them. 如果将javascript函数移动到外部文件中,然后从页面头部分链接到该文件,则验证器不必担心它们。

eg 例如

<script src="functions.js" type="text/javascript"></script>

This also allows you to reuse functions across multiple pages without repeating the code. 这也使您可以在多个页面上重用功能,而无需重复代码。

A further benefit is that the user's browser will cache the js file the first time it encounters it, so on subsequent pages the javascript functions file will be retrieved from cache, rather than being downloaded with the page. 另一个好处是,用户的浏览器将在第一次遇到js文件时对其进行缓存,因此在后续页面上,将从缓存中检索javascript函数文件,而不是与页面一起下载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM