The rules for writing XHTML code are much stricter than writing HTML, such as the following code:
1 <Scripttype= "Text/javascript">2 functionCompare (A, b) {3 if(A<b) {4 Alert ("A is less than B");5 } Else if(A>b) {6 Alert ("A is greater than B");7 } Else{8 Alert ("A is equal to B");9 }Ten } One </Script>
This code is valid in HTML, but it is not valid in XHTML. The less than sign (<) in the comparison statement a < b here will be treated as starting a new tag resolution in XHTML. However, as a label, there can be no space behind the less than sign, so this will cause a syntax error.
Solution:
① uses HTML entities (<) in place of all the less-than-sign (<) in the code, although it solves the problem, but it affects the understanding of the Code;
② uses CDATA fragments, as follows:
1 <Scripttype= "Text/javascript"> <! [cdata[ 2 functionCompare (A, b) {3 if(A<b) {4 Alert ("A is less than B");5 } Else if(A>b) {6 Alert ("A is greater than B");7 } Else{8 Alert ("A is equal to B");9 }Ten } One ]]> </Script>
This method can be used to solve this problem in XHTML-compatible browsers. However, there are many browsers that are incompatible with XHTML and therefore do not support CDATA fragments. Therefore, in order to be compatible with all browsers, you can use the following methods:
1 <Scripttype= "Text/javascript">2 //<! [Cdata[3 functionCompare (A, b) {4 if(A<b) {5 Alert ("A is less than B");6 } Else if(A>b) {7 Alert ("A is greater than B");8 } Else{9 Alert ("A is equal to B");Ten } One } A //]]> - </Script>
The use of <script> elements in XHTML