简体   繁体   English

外部JS文件在XHTML中不起作用

[英]External JS file not working in XHTML

I have a file named Reservation.xhtml and in it, I have the script tag in which the external js file is called.The Reservation.xhtml contains a form that i have downloaded from a website and also its javascript which i saved into "validate.js" 我有一个名为Reservation.xhtml的文件,其中有一个脚本标记,其中调用了外部js文件。Reservation.xhtml包含我从网站下载的表单以及保存在“验证”中的javascript。 .js文件”

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

When using Reservation.xhtml, the functions in the validate.js are not being called but when i change the extension of Reservation.xhtml to Reservation.html..The form works fine..So can anybody tell me why the validate.js works in the Reservation.html and not in the Reservation.xhtml.. Please suggest me how to make it work in Reservation.xhtml 当使用Reservation.xhtml时,不会调用validate.js中的函数,而是当我将Reservation.xhtml的扩展名更改为Reservation.html时。该窗体可以正常工作。所以有人可以告诉我validate.js为什么起作用在Reservation.html中而不是Reservation.xhtml中。请建议我如何使其在Reservation.xhtml中工作

According to my tests , there is nothing wrong with the thing you're doing. 根据我的测试 ,您所做的事情没有错。

I created such file: 我创建了这样的文件:

<!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>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      <title>Strict document</title>
    </head>
    <body>
      <p>This is a strict XHTML document.</p> 
      <div id="validate" style="color:red"><tt>validate.js</tt> has not been called!</div>
      <script src="validate.js" type="text/javascript"></script>
    </body>
  </html>

Linked script would alter the innerHTML and style of the div - and it worked in Firefox. 链接脚本将改变div的innerHTML和样式-并且它在Firefox中有效。

Now while it seems that the file extension and doctype do not matter, there are things that do. 现在,虽然文件扩展名和doctype似乎无关紧要,但有些事情确实起作用。
Specifically, tagName which is a HTMLElement property, is different in HTML and XHTML: 具体来说,作为HTMLElement属性的tagName在HTML和XHTML中有所不同:

In XHTML (or any other XML format), "span" would be alerted. 在XHTML(或任何其他XML格式)中,将警告“ span”。 In HTML, "SPAN" would be alerted instead. 在HTML中,将改为警告“ SPAN”。 - MDN -MDN

This might cause your script not to perform some operations. 这可能会导致脚本不执行某些操作。

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

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