2009年7月22日

 HTML中<,>,&等有特殊含义(<,>,用于链接签,&用于转义),不能直接使用。这些符号是不显示在我们最终看到的网页里的,那如果我们希望在网页中显示这些符号,该怎么办呢?

这就要说到HTML转义字符串(Escape Sequence)了。

转义字符串 (Escape Sequence)也称字符实体(Character Entity)。在HTML中,定义转义字符串的原因有两个:第一个原因是像“<”和“>”这类符号已经用来表示HTML标签,因此就不能直接当作文本中的符号来使用。为了在HTML文档中使用这些符号,就需要定义它的转义字符串。当解释程序遇到这类字符串时就把它解释为真实的字符。在输入转义字符串时,要严格遵守字母大小写的规则。第二个原因是,有些字符在ASCII字符集中没有定义,因此需要使用转义字符串来表示。

转义字符串的组成

转义字符串(Escape Sequence),即字符实体(Character Entity)分成三部分:第一部分是一个&符号,英文叫ampersand;第二部分是实体(Entity)名字或者是#加上实体(Entity)编号;第三部分是一个分号。

比如,要显示小于号(<),就可以写 &lt; 或者 &#60; 。

用实体(Entity)名字的好处是比较好理解,一看lt,大概就猜出是less than的意思,但是其劣势在于并不是所有的浏览器都支持最新的Entity名字。而实体(Entity)编号,各种浏览器都能处理。

提示:实体名称(Entity)是区分大小写的。

备注:同一个符号,可以用“实体名称”和“实体编号”两种方式引用,“实体名称”的优势在于便于记忆,但不能保证所有的浏览器都能顺利识别它,而“实体编号”则没有这种担忧,但它实在不方便记忆。

如何显示空格?

通常情况下,HTML会自动截去多余的空格。不管你加多少空格,都被看做一个空格。比如你在两个字之间加了10个空格,HTML会截去9个空格,只保留一个。为了在网页中增加空格,你可以使用&nbsp;表示空格。


HTML特殊转义字符列表

最常用的字符实体
Character Entities

显示 说明 实体名称 实体编号
半方大的空白 &ensp; &#8194;
全方大的空白 &emsp; &#8195;
不断行的空白格 &nbsp; &#160;
< 小于 &lt; &#60;
> 大于 &gt; &#62;
& &符号 &amp; &#38;
" 双引号 &quot; &#34;
© 版权 &copy; &#169;
® 已注册商标 &reg; &#174;
商标(美国) &#8482;
× 乘号 &times; &#215;
÷ 除号 &divide; &#247;

ISO 8859-1 (Latin-1)字符集

HTML 4.01 支持 ISO 8859-1 (Latin-1) 字符集。

备注:为了方便起见,以下表格中,“实体名称”简称为“名称”,“实体编号”简称为“编号”

显示 名称 编号 显示 名称 编号 显示 名称 编号 显示 名称 编号 显示 名称 编号
  &nbsp; &#160; ¡ &iexcl; &#161; ¢ &cent; &#162; £ &pound; &#163; ¤ &curren; &#164;
¥ &yen; &#165; ¦ &brvbar; &#166; § &sect; &#167; ¨ &uml; &#168; © &copy; &#169;
ª &ordf; &#170; « &laquo; &#171; ¬ &not; &#172; ­ &shy; &#173; ® &reg; &#174;
¯ &macr; &#175; ° &deg; &#176; ± &plusmn; &#177; ² &sup2; &#178; ³ &sup3; &#179;
´ &acute; &#180; µ &micro; &#181; &para; &#182; · &middot; &#183; ¸ &cedil; &#184;
¹ &sup1; &#185; º &ordm; &#186; » &raquo; &#187; ¼ &frac14; &#188; ½ &frac12; &#189;
¾ &frac34; &#190; ¿ &iquest; &#191; À &Agrave; &#192; Á &Aacute; &#193; Â &Acirc; &#194;
à &Atilde; &#195; Ä &Auml; &#196; Å &Aring; &#197; Æ &AElig; &#198; Ç &Ccedil; &#199;
È &Egrave; &#200; É &Eacute; &#201; Ê &Ecirc; &#202; Ë &Euml; &#203; Ì &Igrave; &#204;
Í &Iacute; &#205; Î &Icirc; &#206; Ï &Iuml; &#207; Ð &ETH; &#208; Ñ &Ntilde; &#209;
Ò &Ograve; &#210; Ó &Oacute; &#211; Ô &Ocirc; &#212; Õ &Otilde; &#213; Ö &Ouml; &#214;
× &times; &#215; Ø &Oslash; &#216; Ù &Ugrave; &#217; Ú &Uacute; &#218; Û &Ucirc; &#219;
Ü &Uuml; &#220; Ý &Yacute; &#221; Þ &THORN; &#222; ß &szlig; &#223; à &agrave; &#224;
á &aacute; &#225; â &acirc; &#226; ã &atilde; &#227; ä &auml; &#228; å &aring; &#229;
æ &aelig; &#230; ç &ccedil; &#231; è &egrave; &#232; é &eacute; &#233; ê &ecirc; &#234;
ë &euml; &#235; ì &igrave; &#236; í &iacute; &#237; î &icirc; &#238; ï &iuml; &#239;
ð &eth; &#240; ñ &ntilde; &#241; ò &ograve; &#242; ó &oacute; &#243; ô &ocirc; &#244;
õ &otilde; &#245; ö &ouml; &#246; ÷ &divide; &#247; ø &oslash; &#248; ù &ugrave; &#249;
ú &uacute; &#250; û &ucirc; &#251; ü &uuml; &#252; ý &yacute; &#253; þ &thorn; &#254;
ÿ &yuml; &#255;

数学和希腊字母标志
symbols, mathematical symbols, and Greek letters

显示 名称 编号 显示 名称 编号 显示 名称 编号 显示 名称 编号 显示 名称 编号
ƒ &fnof; &#402; Α &Alpha; &#913; Β &Beta; &#914; Γ &Gamma; &#915; Δ &Delta; &#916;
Ε &Epsilon; &#917; Ζ &Zeta; &#918; Η &Eta; &#919; Θ &Theta; &#920; Ι &Iota; &#921;
Κ &Kappa; &#922; Λ &Lambda; &#923; Μ &Mu; &#924; Ν &Nu; &#925; Ξ &Xi; &#926;
Ο &Omicron; &#927; Π &Pi; &#928; Ρ &Rho; &#929; Σ &Sigma; &#931; Τ &Tau; &#932;
Υ &Upsilon; &#933; Φ &Phi; &#934; Χ &Chi; &#935; Ψ &Psi; &#936; Ω &Omega; &#937;
α &alpha; &#945; β &beta; &#946; γ &gamma; &#947; δ &delta; &#948; ε &epsilon; &#949;
ζ &zeta; &#950; η &eta; &#951; θ &theta; &#952; ι &iota; &#953; κ &kappa; &#954;
λ &lambda; &#955; μ &mu; &#956; ν &nu; &#957; ξ &xi; &#958; ο &omicron; &#959;
π &pi; &#960; ρ &rho; &#961; ς &sigmaf; &#962; σ &sigma; &#963; τ &tau; &#964;
υ &upsilon; &#965; φ &phi; &#966; χ &chi; &#967; ψ &psi; &#968; ω &omega; &#969;
? &thetasym; &#977; ? &upsih; &#978; ? &piv; &#982; &bull; &#8226; &hellip; &#8230;
&prime; &#8242; &Prime; &#8243; &oline; &#8254; &frasl; &#8260; &weierp; &#8472;
&image; &#8465; &real; &#8476; &trade; &#8482; &alefsym; &#8501; &larr; &#8592;
&uarr; &#8593; &rarr; &#8594; &darr; &#8595; &harr; &#8596; &crarr; &#8629;
&lArr; &#8656; &uArr; &#8657; &rArr; &#8658; &dArr; &#8659; &hArr; &#8660;
&forall; &#8704; &part; &#8706; &exist; &#8707; &empty; &#8709; &nabla; &#8711;
&isin; &#8712; &notin; &#8713; &ni; &#8715; &prod; &#8719; &sum; &#8721;
&minus; &#8722; &lowast; &#8727; &radic; &#8730; &prop; &#8733; &infin; &#8734;
&ang; &#8736; &and; &#8743; &or; &#8744; &cap; &#8745; &cup; &#8746;
&int; &#8747; &there4; &#8756; &sim; &#8764; &cong; &#8773; &asymp; &#8776;
&ne; &#8800; &equiv; &#8801; &le; &#8804; &ge; &#8805; &sub; &#8834;
&sup; &#8835; &nsub; &#8836; &sube; &#8838; &supe; &#8839; &oplus; &#8853;
&otimes; &#8855; &perp; &#8869; &sdot; &#8901; ? &lceil; &#8968; ? &rceil; &#8969;
? &lfloor; &#8970; ? &rfloor; &#8971; ? &lang; &#9001; ? &rang; &#9002; &loz; &#9674;
&spades; &#9824; &clubs; &#9827; &hearts; &#9829; &diams; &#9830;

重要的国际标记
markup-significant and internationalization characters

显示 名称 编号 显示 名称 编号 显示 名称 编号 显示 名称 编号 显示 名称 编号
" &quot; &#34; & &amp; &#38; < &lt; &#60; > &gt; &#62; Œ &OElig; &#338;
œ &oelig; &#339; Š &Scaron; &#352; š &scaron; &#353; Ÿ &Yuml; &#376; ˆ &circ; &#710;
˜ &tilde; &#732; &ensp; &#8194; &emsp; &#8195; &thinsp; &#8201; &zwnj; &#8204;
&zwj; &#8205; &lrm; &#8206; &rlm; &#8207; &ndash; &#8211; &mdash; &#8212;
&lsquo; &#8216; &rsquo; &#8217; &sbquo; &#8218; &ldquo; &#8220; &rdquo; &#8221;
&bdquo; &#8222; &dagger; &#8224; &Dagger; &#8225; &permil; &#8240; &lsaquo; &#8249;
&rsaquo; &#8250; &euro; &#8364;

JavaScript转义符

转义序列 字符
\b 退格
\f 走纸换页
\n 换行
\r 回车
\t 横向跳格 (Ctrl-I)
\' 单引号
\" 双引号
\\ 反斜杠

编程的时候要注意特殊字符的问题,很多运行时出现的问题都是因为特殊字符的出现而引起的。

注意,由于反斜杠本身用作转义符,因此不能直接在脚本中键入一个反斜杠。如果要产生一个反斜杠,必须一起键入两个反斜杠 (\\)。


编码转换(to Unicode)

(程序代码来源于网络)

Js版

<script>
     test = "你好abc"
     str = ""
     for( i=0;    i<test.length; i++ )
     {
      temp = test.charCodeAt(i).toString(16);
      str    += "\\u"+ new Array(5-String(temp).length).join("0") +temp;
     }
     document.write (str)
</script>


vbs版


Function Unicode(str1)
     Dim str,temp
     str = ""
     For i=1    to len(str1)
      temp = Hex(AscW(Mid(str1,i,1)))
      If len(temp) < 5 Then    temp = right("0000" & temp, 4)
      str = str & "\u" & temp
     Next
     Unicode = str
End Function


Function htmlentities(str)
     For i = 1 to Len(str)
         char = mid(str, i, 1)
         If Ascw(char) > 128 then
             htmlentities = htmlentities & "&#" & Ascw(char) & ";"
         Else
             htmlentities = htmlentities & char
         End if
     Next
End Function

coldfusion

function nochaoscode(str)
{
     var new_str = “”;
     for(i=1; i lte len(str);i=i+1){
         if(asc(mid(str,i,1)) lt 128){
             new_str = new_str & mid(str,i,1);
         }else{
             new_str = new_str & “&##” & asc(mid(str,i,1));
         }
     }
     return new_str;
}


附:

在php中我们可以用mbstring的mb_convert_encoding函数实现这个正向及反向的转化。 如:


mb_convert_encoding ("你好", "HTML-ENTITIES", "gb2312"); //输出:&#20320;&#22909;

mb_convert_encoding ("&#20320;&#22909;", "gb2312", "HTML-ENTITIES"); //输出:你好

如果需要对整个页面转化,则只需要在php文件的头部加上这三行代码:

mb_internal_encoding("gb2312"); // 这里的gb2312是你网站原来的编码

mb_http_output("HTML-ENTITIES");

ob_start('mb_output_handler');

posted @ 2009-08-05 15:32 zhuyongjp 阅读(1841) | 评论 (0)编辑 收藏
 

keycode    8 = BackSpace BackSpace
keycode    9 = Tab Tab
keycode   12 = Clear
keycode   13 = Enter
keycode   16 = Shift_L
keycode   17 = Control_L
keycode   18 = Alt_L
keycode   19 = Pause
keycode   20 = Caps_Lock
keycode   27 = Escape Escape
keycode   32 = space space
keycode   33 = Prior
keycode   34 = Next
keycode   35 = End
keycode   36 = Home
keycode   37 = Left
keycode   38 = Up
keycode   39 = Right
keycode   40 = Down
keycode   41 = Select
keycode   42 = Print
keycode   43 = Execute
keycode   45 = Insert
keycode   46 = Delete
keycode   47 = Help
keycode   48 = 0 equal braceright
keycode   49 = 1 exclam onesuperior
keycode   50 = 2 quotedbl twosuperior
keycode   51 = 3 section threesuperior
keycode   52 = 4 dollar
keycode   53 = 5 percent
keycode   54 = 6 ampersand
keycode   55 = 7 slash braceleft
keycode   56 = 8 parenleft bracketleft
keycode   57 = 9 parenright bracketright
keycode   65 = a A
keycode   66 = b B
keycode   67 = c C
keycode   68 = d D
keycode   69 = e E EuroSign
keycode   70 = f F
keycode   71 = g G
keycode   72 = h H
keycode   73 = i I
keycode   74 = j J
keycode   75 = k K
keycode   76 = l L
keycode   77 = m M mu
keycode   78 = n N
keycode   79 = o O
keycode   80 = p P
keycode   81 = q Q at
keycode   82 = r R
keycode   83 = s S
keycode   84 = t T
keycode   85 = u U
keycode   86 = v V
keycode   87 = w W
keycode   88 = x X
keycode   89 = y Y
keycode   90 = z Z
keycode   96 = KP_0 KP_0
keycode   97 = KP_1 KP_1
keycode   98 = KP_2 KP_2
keycode   99 = KP_3 KP_3
keycode 100 = KP_4 KP_4
keycode 101 = KP_5 KP_5
keycode 102 = KP_6 KP_6
keycode 103 = KP_7 KP_7
keycode 104 = KP_8 KP_8
keycode 105 = KP_9 KP_9
keycode 106 = KP_Multiply KP_Multiply
keycode 107 = KP_Add KP_Add
keycode 108 = KP_Separator KP_Separator
keycode 109 = KP_Subtract KP_Subtract
keycode 110 = KP_Decimal KP_Decimal
keycode 111 = KP_Divide KP_Divide
keycode 112 = F1
keycode 113 = F2
keycode 114 = F3
keycode 115 = F4
keycode 116 = F5
keycode 117 = F6
keycode 118 = F7
keycode 119 = F8
keycode 120 = F9
keycode 121 = F10
keycode 122 = F11
keycode 123 = F12
keycode 124 = F13
keycode 125 = F14
keycode 126 = F15
keycode 127 = F16
keycode 128 = F17
keycode 129 = F18
keycode 130 = F19
keycode 131 = F20
keycode 132 = F21
keycode 133 = F22
keycode 134 = F23
keycode 135 = F24
keycode 136 = Num_Lock
keycode 137 = Scroll_Lock
keycode 187 = acute grave
keycode 188 = comma semicolon
keycode 189 = minus underscore
keycode 190 = period colon
keycode 192 = numbersign apostrophe
keycode 210 = plusminus hyphen macron
keycode 211 =
keycode 212 = copyright registered
keycode 213 = guillemotleft guillemotright
keycode 214 = masculine ordfeminine
keycode 215 = ae AE
keycode 216 = cent yen
keycode 217 = questiondown exclamdown
keycode 218 = onequarter onehalf threequarters
keycode 220 = less greater bar
keycode 221 = plus asterisk asciitilde
keycode 227 = multiply division
keycode 228 = acircumflex Acircumflex
keycode 229 = ecircumflex Ecircumflex
keycode 230 = icircumflex Icircumflex
keycode 231 = ocircumflex Ocircumflex
keycode 232 = ucircumflex Ucircumflex
keycode 233 = ntilde Ntilde
keycode 234 = yacute Yacute
keycode 235 = oslash Ooblique
keycode 236 = aring Aring
keycode 237 = ccedilla Ccedilla
keycode 238 = thorn THORN
keycode 239 = eth ETH
keycode 240 = diaeresis cedilla currency
keycode 241 = agrave Agrave atilde Atilde
keycode 242 = egrave Egrave
keycode 243 = igrave Igrave
keycode 244 = ograve Ograve otilde Otilde
keycode 245 = ugrave Ugrave
keycode 246 = adiaeresis Adiaeresis
keycode 247 = ediaeresis Ediaeresis
keycode 248 = idiaeresis Idiaeresis
keycode 249 = odiaeresis Odiaeresis
keycode 250 = udiaeresis Udiaeresis
keycode 251 = ssharp question backslash
keycode 252 = asciicircum degree
keycode 253 = 3 sterling
keycode 254 = Mode_switch

字母和数字键的键码值(keyCode)
按键 键码 按键 键码 按键 键码 按键 键码
  A
 65 J 74 S 83 1 49
B 66 K 75 T 84 2 50
C 67 L 76 U 85 3 51
D 68 M 77 V 86 4 52
E 69 N 78 W 87 5 53
F 70 O 79 X 88 6 54
G 71 P 80 Y 89 7 55
H 72 Q 81 Z 90 8 56
I 73 R 82 0 48 9 57
数字键盘上的键的键码值(keyCode) 功能键键码值(keyCode)
按键 键码 按键 键码 按键 键码 按键 键码
  0
 96 8 104 F1 112 F7 118
1 97 9 105 F2 113 F8 119
2 98 * 106 F3 114 F9 120
3 99 + 107 F4 115 F10 121
4 100 Enter 108 F5 116 F11 122
5 101 - 109 F6 117 F12 123
6 102   .
 110        
7   103
 / 111        
控制键键码值(keyCode)
按键 键码 按键 键码 按键 键码 按键 键码
BackSpace 8 Esc 27 Right Arrow 39 -_ 189
Tab 9 Spacebar 32 Down Arrow 40 .> 190
Clear 12 Page Up 33 Insert 45 /? 191
Enter 13 Page Down 34 Delete 46 `~ 192
Shift 16 End 35 Num Lock 144 [{ 219
Control 17 Home 36 ;: 186 \| 220
Alt 18 Left Arrow 37 =+ 187 ]} 221
Cape Lock 20 Up Arrow 38 ,< 188 '" 222

posted @ 2009-07-23 17:57 zhuyongjp 阅读(2072) | 评论 (0)编辑 收藏
 
javascript 如何创建一个 DOM 节点?





key
-------------------------------------------
1、a.创建一个Element             var newDiv = document.createElement("div");
   b.设置它的属性                newDiv.id = "divNew";
       c.如果需要,创建子节点      var newTable = document.createElement("table");
       d.设置子节点属性           newTable.setAttribute("id","tableNew");
       e.重复步骤c和d,直到完成整个目录树
       f.创建Text节点            var newText = document.createTextNode("string");
  g.从叶子节点往上appendChild     newDiv.appendChild(newTable);
                                document.body.appendChild(newDiv);

另一种方式
    a.创建一个Element            var newDiv = document.createElement("div");
    b.用 innerHTML                                newDiv.innerHTML="<table><tr><th>type</th><th>value</th></tr></table>";
posted @ 2009-07-22 15:27 zhuyongjp 阅读(1327) | 评论 (1)编辑 收藏