简体   繁体   English

W3用1行JavaScript代码验证html错误

[英]W3 validate html error with 1 line of javascript code

I get the following error with validator.W3.org 我在validator.W3.org中收到以下错误

Line 70, Column 26: character "<" is the first character of a delimiter but occurred as data 第70行,第26列:字符“ <”是定界符的第一个字符,但作为数据出现

if (remainingSeconds < 10) {

Line 70, Column 26: StartTag: invalid element name 第70行,第26列:StartTag:无效的元素名称

if (remainingSeconds < 10) {

This is the code I use. 这是我使用的代码。

<script type="text/javascript">
function secondPassed() {
var minutes = Math.round((seconds - 30)/60);
var remainingSeconds = seconds % 60;
if (remainingSeconds < 10) {
    remainingSeconds = "0" + remainingSeconds;  
}
</script>

If I delete the < or change the < to a = then the error is gone. 如果删除<或将<更改为a =,则错误消失了。

Anybody an idea? 有人有主意吗?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

You're validating an XHTML document, so you have to use CDATA markers around your script contents . 您正在验证XHTML文档,因此必须在脚本内容周围使用CDATA标记

<script>
  <![CDATA[
  // JavaScript goes here
  ]]>
</script>

Personally I don't know why you'd use XHTML in this day and age, but whatever. 就我个人而言,我不知道为什么您会在当今时代使用XHTML,但无论如何。

If you are validating as XHTML, try wrapping your JavaScript in <![CDATA[]]> blocks: 如果您要验证为XHTML,请尝试将JavaScript包装在<![CDATA[]]>块中:

What does <![CDATA[]]> in XML mean? XML中的<![CDATA []]>是什么意思?

You're using strict XHTML, so all characters that are part of the XML syntax (pretty much) have to be escaped properly if you want to use them as text. 您使用的是严格的XHTML,因此,如果要将XML语法中的所有字符用作文本,则必须正确地对其进行转义。 In XHTML, the usual way to do that is with a CDATA block that's escaped in a way to be compatible with HTML: 在XHTML中,通常的做法是使用CDATA块,该块以与HTML兼容的方式进行了转义:

<script type="text/javascript">//<![CDATA[
function secondPassed() {
var minutes = Math.round((seconds - 30)/60);
var remainingSeconds = seconds % 60;
if (remainingSeconds < 10) {
    remainingSeconds = "0" + remainingSeconds;  
}
//]]></script>

However, there are two better fixes: 但是,有两个更好的修复程序:

  • Use HTML5 ( <!DOCTYPE html> ), not XHTML 使用HTML5( <!DOCTYPE html> ),而不是XHTML
  • Use an external script, <script type="text/javascript" src="second-passed.js"></script> 使用外部脚本<script type="text/javascript" src="second-passed.js"></script>

Both are better practice. 两者都是更好的做法。

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

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