如下代码:
<script type="text/javascript">
function compare(a,b){
if(a<b){
alert("A is less than B")、、、
} else if(a>b){
alert("A is greater than B");
} else{
alert("A is equal to B");
}
}
</script>
在html中有效,在xhtml中无效,<符号会被解析标签起始符,解决的办法一:
<script type="text/javascript">
function compare(a,b){
if(a < b){
alert("A is less than B")、、、
} else if(a>b){
alert("A is greater than 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 than B")、、、
} else if(a>b){
alert("A is greater than B");
} else{
alert("A is equal to B");
}
}
]]></script> 使用CDATA片段来包含javascript代码
但是如果浏览器不支持XHTML,则修改代码如下:
<script type="text/javascript">
//<![CDATA[
function compare(a,b){
if(a<b){
alert("A is less than B")、、、
} else if(a>b){
alert("A is greater than B");
} else{
alert("A is equal to B");
}
}
//]]>
</script>
这种格式在所有现代浏览器中都可以使用。