JavaScript在XHTML中的用法詳解
更新時(shí)間:2013年04月11日 16:36:06 作者:
下面的代碼在HTML中是有效的,但在XHTML中則是無(wú)效的接下來(lái)為大家介紹下JavaScript在XHTML中的用法,感興趣的朋友可以參考下哈
編寫(xiě)XHTML代碼的規(guī)則要比編寫(xiě)HTML要嚴(yán)格得多,類(lèi)似下面的代碼在HTML中是有效的,但在XHTML中則是無(wú)效的。
[javascript]
<script type="text/javascript">
function compare(a, b)
{
if(a < b)
{
alert("a is less then b");
}
else if(a > b)
{
alert("a is greater then b");
}
else
{
alert("a is equal to b");
}
}
</script>
<script type="text/javascript">
function compare(a, b)
{
if(a < b)
{
alert("a is less then b");
}
else if(a > b)
{
alert("a is greater then b");
}
else
{
alert("a is equal to b");
}
}
</script>
在HTML中,有特殊的規(guī)則用以確定<script>元素中的哪些內(nèi)容可以被解析,但這些規(guī)則在XHTML中不適用。因?yàn)樾∮谔?hào)(<)在XHTML中將被當(dāng)作開(kāi)始一個(gè)新標(biāo)簽來(lái)解析。但是作為標(biāo)簽,小于號(hào)后面不能跟空格,因此導(dǎo)致語(yǔ)法錯(cuò)誤。
解決方法有兩個(gè):一、用相應(yīng)的HTML實(shí)體(<)替換代碼中所有的小于號(hào)(<);二、使用一個(gè)CData片段來(lái)包含JavaScript代碼。
方法一相應(yīng)代碼:
[javascript]
<script type="text/javascript">
function compare(a, b)
{
if(a < b)
{
alert("a is less then b");
}
else if(a > b)
{
alert("a is greater then b");
}
else
{
alert("a is equal to b");
}
}
</script>
<script type="text/javascript">
function compare(a, b)
{
if(a < b)
{
alert("a is less then b");
}
else if(a > b)
{
alert("a is greater then b");
}
else
{
alert("a is equal to b");
}
}
</script>
方法二相應(yīng)代碼:
[javascript]
<script type="text/javascript"><![CDATA[
function compare(a, b)
{
if(a < b)
{
alert("a is less then b");
}
else if(a > b)
{
alert("a is greater then b");
}
else
{
alert("a is equal to b");
}
}
]]></script>
<script type="text/javascript"><![CDATA[
function compare(a, b)
{
if(a < b)
{
alert("a is less then b");
}
else if(a > b)
{
alert("a is greater then b");
}
else
{
alert("a is equal to b");
}
}
]]></script>
方法一雖然可以讓代碼在XHTML中正常運(yùn)行,但卻導(dǎo)致代碼不好理解了;而方法二在兼容XHTML的瀏覽器中可以解決問(wèn)題。但不少瀏覽器并不兼容XHTML,因而不支持CData片段。所以再使用JavaScript注釋將CData標(biāo)記注釋掉。
相應(yīng)代碼:
[html]
<script type="text/javascript">
//<![CDATA[
function compare(a, b)
{
if(a < b)
{
alert("a is less then b");
}
else if(a > b)
{
alert("a is greater then b");
}
else
{
alert("a is equal to b");
}
}
//]]>
</script>
<script type="text/javascript">
//<![CDATA[
function compare(a, b)
{
if(a < b)
{
alert("a is less then b");
}
else if(a > b)
{
alert("a is greater then b");
}
else
{
alert("a is equal to b");
}
}
//]]>
</script>
這種格式在所有現(xiàn)代瀏覽器中都可以正常使用。
附:不推薦使用的語(yǔ)法
[javascript]
<script><!--
function sayHi(){
alert("Hi!);
}
//--></script>
<script><!--
function sayHi(){
alert("Hi!);
}
//--></script>
像上面這樣把JavaScript代碼包含在一個(gè)HTML注釋中可以讓不支持<script>元素的瀏覽器隱藏嵌入的JavaScript代碼,即忽略<script>標(biāo)簽中的內(nèi)容,而那些支持JavaScript的瀏覽器在遇到這種情況時(shí),則必須進(jìn)一步確認(rèn)其中是否包含需要解析的JavaScript代碼。
雖然這種注釋格式得到了所有瀏覽器的認(rèn)可,也能被正確的解釋,但由于所有瀏覽器都已經(jīng)支持JavaScript,因此也就沒(méi)有必要再使用這種格式了。
[javascript]
復(fù)制代碼 代碼如下:
<script type="text/javascript">
function compare(a, b)
{
if(a < b)
{
alert("a is less then b");
}
else if(a > b)
{
alert("a is greater then b");
}
else
{
alert("a is equal to b");
}
}
</script>
<script type="text/javascript">
function compare(a, b)
{
if(a < b)
{
alert("a is less then b");
}
else if(a > b)
{
alert("a is greater then b");
}
else
{
alert("a is equal to b");
}
}
</script>
在HTML中,有特殊的規(guī)則用以確定<script>元素中的哪些內(nèi)容可以被解析,但這些規(guī)則在XHTML中不適用。因?yàn)樾∮谔?hào)(<)在XHTML中將被當(dāng)作開(kāi)始一個(gè)新標(biāo)簽來(lái)解析。但是作為標(biāo)簽,小于號(hào)后面不能跟空格,因此導(dǎo)致語(yǔ)法錯(cuò)誤。
解決方法有兩個(gè):一、用相應(yīng)的HTML實(shí)體(<)替換代碼中所有的小于號(hào)(<);二、使用一個(gè)CData片段來(lái)包含JavaScript代碼。
方法一相應(yīng)代碼:
[javascript]
復(fù)制代碼 代碼如下:
<script type="text/javascript">
function compare(a, b)
{
if(a < b)
{
alert("a is less then b");
}
else if(a > b)
{
alert("a is greater then b");
}
else
{
alert("a is equal to b");
}
}
</script>
<script type="text/javascript">
function compare(a, b)
{
if(a < b)
{
alert("a is less then b");
}
else if(a > b)
{
alert("a is greater then b");
}
else
{
alert("a is equal to b");
}
}
</script>
方法二相應(yīng)代碼:
[javascript]
復(fù)制代碼 代碼如下:
<script type="text/javascript"><![CDATA[
function compare(a, b)
{
if(a < b)
{
alert("a is less then b");
}
else if(a > b)
{
alert("a is greater then b");
}
else
{
alert("a is equal to b");
}
}
]]></script>
<script type="text/javascript"><![CDATA[
function compare(a, b)
{
if(a < b)
{
alert("a is less then b");
}
else if(a > b)
{
alert("a is greater then b");
}
else
{
alert("a is equal to b");
}
}
]]></script>
方法一雖然可以讓代碼在XHTML中正常運(yùn)行,但卻導(dǎo)致代碼不好理解了;而方法二在兼容XHTML的瀏覽器中可以解決問(wèn)題。但不少瀏覽器并不兼容XHTML,因而不支持CData片段。所以再使用JavaScript注釋將CData標(biāo)記注釋掉。
相應(yīng)代碼:
[html]
復(fù)制代碼 代碼如下:
<script type="text/javascript">
//<![CDATA[
function compare(a, b)
{
if(a < b)
{
alert("a is less then b");
}
else if(a > b)
{
alert("a is greater then b");
}
else
{
alert("a is equal to b");
}
}
//]]>
</script>
<script type="text/javascript">
//<![CDATA[
function compare(a, b)
{
if(a < b)
{
alert("a is less then b");
}
else if(a > b)
{
alert("a is greater then b");
}
else
{
alert("a is equal to b");
}
}
//]]>
</script>
這種格式在所有現(xiàn)代瀏覽器中都可以正常使用。
附:不推薦使用的語(yǔ)法
[javascript]
復(fù)制代碼 代碼如下:
<script><!--
function sayHi(){
alert("Hi!);
}
//--></script>
<script><!--
function sayHi(){
alert("Hi!);
}
//--></script>
像上面這樣把JavaScript代碼包含在一個(gè)HTML注釋中可以讓不支持<script>元素的瀏覽器隱藏嵌入的JavaScript代碼,即忽略<script>標(biāo)簽中的內(nèi)容,而那些支持JavaScript的瀏覽器在遇到這種情況時(shí),則必須進(jìn)一步確認(rèn)其中是否包含需要解析的JavaScript代碼。
雖然這種注釋格式得到了所有瀏覽器的認(rèn)可,也能被正確的解釋,但由于所有瀏覽器都已經(jīng)支持JavaScript,因此也就沒(méi)有必要再使用這種格式了。
相關(guān)文章
Javascript開(kāi)發(fā)之三數(shù)組對(duì)象實(shí)例介紹
Javascript開(kāi)發(fā)之三組數(shù)對(duì)象詳細(xì)介紹,需要的朋友可以參考下2012-11-11JavaScript lastIndexOf方法入門(mén)實(shí)例(計(jì)算指定字符在字符串中最后一次出現(xiàn)的位置)
這篇文章主要介紹了JavaScript字符串對(duì)象的lastIndexOf方法入門(mén)實(shí)例,lastIndexOf方法用于計(jì)算指定字符在字符串中最后一次出現(xiàn)的位置,需要的朋友可以參考下2014-10-10