代码之家  ›  专栏  ›  技术社区  ›  Cesar

在HTML问题中添加脚本JS

  •  4
  • Cesar  · 技术社区  · 17 年前

    有人能回答我为什么会出现这个问题吗?

    使用以下代码,我的网站只能在Chrome浏览器中工作,在IE7/8和Firefox上,只显示背景:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Site Title</title>
    <link href="css/styles.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="js/jquery-1.3.2.min.js" />
    <script type="text/javascript" src="js/functions.js" />
    </head>
    ...
    

    在Chrome、IE7/8和Firefox中使用此代码:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" lang="pt-br" xml:lang="pt-br">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>CSite Title</title>
    <link href="css/styles.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
    <script type="text/javascript" src="js/functions.js"></script>
    </head>
    ...
    

    查看Firebug中不起作用的代码时,我发现没有调用文件'functions.js',我不明白,为什么不使用' <script /> “和” <script></script> “有效吗?

    4 回复  |  直到 16 年前
        1
  •  12
  •   Community Mohan Dere    9 年前

    从这张票( Why don't self-closing script tags work? )

    (请注意,在参考票据中,接受的答案实际上并不正确)

    解析。即使您使用XML IE仍然将文档解析为普通文档 不支持自动关闭语法。 后面的斜杠被忽略了, 您必须使用显式结束符 标签

    解析仍将解析文档 使用xml mime类型。但是在那种情况下 IE不会在任何时候显示文档

        2
  •  4
  •   Community Mohan Dere    9 年前

    因为 <script> 标记在HTML4中必须有结束标记

    另一个信息来源: XHTML - Is writing self closing tags for elements not traditionally empty bad practise?

        3
  •  2
  •   tvanfosson    17 年前
        4
  •  1
  •   Roger Roger    17 年前

    关闭标记我认为最好在代码中查看,而且我认为这是一个很好的实践。如果您遵循此提示,将不再有此类问题。