<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>枫芸志 &#187; Javascript</title>
	<atom:link href="http://witmax.cn/tag/javascript/feed" rel="self" type="application/rss+xml" />
	<link>http://witmax.cn</link>
	<description>记录成长路途上的点滴总结</description>
	<lastBuildDate>Sat, 04 Feb 2012 13:47:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>【Javascript】利用document.domain解决子域跨域访问问题</title>
		<link>http://witmax.cn/js-cross-subdomain.html</link>
		<comments>http://witmax.cn/js-cross-subdomain.html#comments</comments>
		<pubDate>Sun, 08 Jan 2012 08:46:03 +0000</pubDate>
		<dc:creator>晴枫</dc:creator>
				<category><![CDATA[网页设计]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://witmax.cn/?p=1890</guid>
		<description><![CDATA[根据浏览器安全策略，不同域名（比如witmax.cn和www.witmax.cn）、不同协议（比如http://witmax.cn和https://witmax.cn）、不同端口（比如http://witmax.cn和http://witmax.cn:8088）之间的页面不能相互访问，包括XMLHTTPRequest方式和Javascript的页面访问。 有一个例外是：在相同根域、相同协议、相同端口的情况下，可以利用设置document.domain方式来解决不同子域的跨站访问问题。 举个例子，想在witmax.cn中，访问www.witmax.cn域名下的内容，需要以下几步来完成： 在主页面（位于witmax.cn中）通过隐藏的iframe嵌套子页面（内容位于www.witmax.cn） 在主页面中设置document.domain=”witmax.cn”; 在子页面中设置document.domain=”witmax.cn”; 在主页面和子页面中可以相互访问内容了；需要跨域访问其他内容，可以通过在对应页面上发请求完成取回数据然后进行对应操作 P.S. 补充几点： 1、document.domain可以由子域www.witmax.cn修改为主域witmax.cn，而不能由witmax.cn修改为www.witmax.cn（不是所有浏览器都支持）。 2、不同根域、或不同协议、或不同端口之间的跨域访问只能通过服务器断的代理程序来解决了。 3、详细的示例见参考文档： 用document.domain解决Ajax跨子域 用document.domain+iframe实现Ajax跨子域 设置 iframe document.domain杂谈]]></description>
			<content:encoded><![CDATA[<p>根据浏览器安全策略，不同域名（比如witmax.cn和www.witmax.cn）、不同协议（比如http://witmax.cn和https://witmax.cn）、不同端口（比如http://witmax.cn和http://witmax.cn:8088）之间的页面不能相互访问，包括XMLHTTPRequest方式和Javascript的页面访问。</p>
<p>有一个例外是：<strong>在相同根域、相同协议、相同端口的情况下，可以利用设置document.domain方式来解决不同子域的跨站访问问题。</strong></p>
<p>举个例子，想在witmax.cn中，访问www.witmax.cn域名下的内容，需要以下几步来完成：</p>
<p><span id="more-1890"></span></p>
<ol>
<li>在主页面（位于witmax.cn中）通过隐藏的iframe嵌套子页面（内容位于www.witmax.cn）</li>
<li>在主页面中设置document.domain=”witmax.cn”;</li>
<li>在子页面中设置document.domain=”witmax.cn”;</li>
<li>在主页面和子页面中可以相互访问内容了；需要跨域访问其他内容，可以通过在对应页面上发请求完成取回数据然后进行对应操作</li>
</ol>
<p>P.S. 补充几点：</p>
<p>1、document.domain可以由子域<a href="http://www.witmax.cn">www.witmax.cn</a>修改为主域witmax.cn，而不能由witmax.cn修改为<a href="http://www.witmax.cn">www.witmax.cn</a>（不是所有浏览器都支持）。</p>
<p>2、不同根域、或不同协议、或不同端口之间的跨域访问只能通过服务器断的代理程序来解决了。</p>
<p>3、详细的示例见参考文档：</p>
<ul>
<li><a href="http://dancewithnet.com/2007/07/22/solve-cross-sub-domain-ajax-with-document-domain/" target="_blank">用document.domain解决Ajax跨子域</a></li>
<li><a href="http://js8.in/443.html" target="_blank">用document.domain+iframe实现Ajax跨子域</a></li>
<li><a href="http://lixinlixin2008.iteye.com/blog/515352" target="_blank">设置 iframe document.domain杂谈</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://witmax.cn/js-cross-subdomain.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>【Javascript】实现字符串的replaceAll方法</title>
		<link>http://witmax.cn/js-replaceall.html</link>
		<comments>http://witmax.cn/js-replaceall.html#comments</comments>
		<pubDate>Thu, 01 Dec 2011 14:58:30 +0000</pubDate>
		<dc:creator>晴枫</dc:creator>
				<category><![CDATA[网页设计]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://witmax.cn/?p=1837</guid>
		<description><![CDATA[Js中string对象只有replace方法，只能替换内容一次，那么就来实现一下replaceAll的方法 具体使用就很简单了]]></description>
			<content:encoded><![CDATA[<div>
<div>Js中string对象只有replace方法，只能替换内容一次，那么就来实现一下replaceAll的方法</div>
<div>
<pre class="brush: jscript; title: ; notranslate">String.prototype.replaceAll  = function(s1,s2){
return this.replace(new RegExp(s1,&quot;gm&quot;),s2);
} </pre>
</div>
</div>
<div>具体使用就很简单了</div>
<div>
<pre class="brush: jscript; title: ; notranslate">var str=&quot;abcabcabc&quot;;
alert(str.replace(&quot;a&quot;, &quot;*&quot;));
alert(str.replaceAll(&quot;a&quot;, &quot;*&quot;));</pre>
</div>
]]></content:encoded>
			<wfw:commentRss>http://witmax.cn/js-replaceall.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>【Javascript】IE和Firefox在DOM解析childNodes时的不同</title>
		<link>http://witmax.cn/js-childnodes.html</link>
		<comments>http://witmax.cn/js-childnodes.html#comments</comments>
		<pubDate>Tue, 25 Oct 2011 15:13:36 +0000</pubDate>
		<dc:creator>晴枫</dc:creator>
				<category><![CDATA[网页设计]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://witmax.cn/?p=1804</guid>
		<description><![CDATA[写js时碰到的一个问题，发现IE8和Firefox在处理childNodes时结果不同：Firefox和IE9会将回车解析生成TextNode类型的DOM节点，而IE8不会。 可以通过以下代码进行验证 &#60;!doctype html&#62; &#60;html&#62; &#60;head&#62;测试childNodes&#60;/head&#62; &#60;body&#62; &#60;div id=&#34;main&#34;&#62; &#60;div id=&#34;div1&#34;&#62;1&#60;/div&#62; &#60;div id=&#34;div2&#34;&#62;2&#60;/div&#62; &#60;/div&#62; &#60;script&#62; var a=document.getElementById(&#34;main&#34;).childNodes; alert(a.length); &#60;/script&#62; &#60;/body&#62;&#60;/html&#62; 提示：你可以先修改部分代码再运行。 用Firebug在Firefox可以看到如下结果，发现childNodes有5个元素，其中的3个都是TextNode，也就是div前后的回车符解析出来的。 在IE9上测试，用开发人员工具查看了一下childNodes结果，发现结果与Firefox表现一致， childNodes有5个元素，其中的3个都是TextNode。如下图： 但在IE8下，用开发人员工具查看了一下childNodes结果，发现childNodes有2个元素，即2个div元素，不包括TextNode。如下图： 至于如何处理这两种差异，只需要在Firefox中判断节点的NodeType即可，所有NodeType值如下： 值 描述 1 Element Node 单元节点 2 Attribute Node 属性节点 3 Text Node 文本节点 4 CDATA Section Node CDATA选择节点 5 Entity Reference Node 实体索引节点 6 Entity Node 实体节点 7 Processing [...]]]></description>
			<content:encoded><![CDATA[<p>写js时碰到的一个问题，发现IE8和Firefox在处理childNodes时结果不同：<strong>Firefox和IE9会将回车解析生成TextNode类型的DOM节点，而IE8不会。</strong></p>
<p>可以通过以下代码进行验证</p>
<div class="runcode">
<p><textarea name="runcode" style="overflow-y:visible;width:540px;font-size:12px" class="runcode_text" id="runcode_1p3EuD">&lt;!doctype html&gt;
&lt;html&gt;
&lt;head&gt;测试childNodes&lt;/head&gt;
&lt;body&gt;
&lt;div id=&quot;main&quot;&gt;
   &lt;div id=&quot;div1&quot;&gt;1&lt;/div&gt;
   &lt;div id=&quot;div2&quot;&gt;2&lt;/div&gt;
&lt;/div&gt;
&lt;script&gt;
 var a=document.getElementById(&quot;main&quot;).childNodes;
 alert(a.length);
&lt;/script&gt;
&lt;/body&gt;&lt;/html&gt;</textarea></p>
<script type="text/javascript">function changeTsize(){document.getElementById("runcode_1p3EuD").style.height = document.getElementById("runcode_1p3EuD").scrollHeight + "px";}window.setTimeout(changeTsize,0);</script><p><input type="button" value="运行" class="runcode_button" onclick="runcode_open_new('runcode_1p3EuD');"/> <input type="button" value="复制" class="runcode_button" onclick="runcode_copy('runcode_1p3EuD');"/> <input type="button" value="另存代码" class="runcode_button" onclick="saveCode('runcode_1p3EuD','runcode_1p3EuD');"/> 提示：你可以先修改部分代码再运行。</p>
</div>
<p><span id="more-1804"></span></p>
<p>用Firebug在Firefox可以看到如下结果，发现childNodes有5个元素，其中的3个都是TextNode，也就是div前后的回车符解析出来的。</p>
<p><div class="wp-caption aligncenter" style="width: 390px"><a title="Firefox下childNodes的表现" href="http://witmax.cn/img/js-childnodes-ff.png" rel="lightbox[childnodes]"><img title="Firefox下childNodes的表现" src="http://witmax.cn/img/js-childnodes-ff.png" alt="Firefox下childNodes的表现" width="380" /></a><p class="wp-caption-text">Firefox下childNodes的表现</p></div>在IE9上测试，用开发人员工具查看了一下childNodes结果，发现结果与Firefox表现一致， childNodes有5个元素，其中的3个都是TextNode。如下图：</p>
<p><div class="wp-caption aligncenter" style="width: 359px"><a title="IE9下childNodes的表现" href="http://witmax.cn/img/js-childnodes-ie9.png" rel="lightbox[childnodes]"><img title="IE9下childNodes的表现" src="http://witmax.cn/img/js-childnodes-ie9.png" alt="IE9下childNodes的表现" width="349" /></a><p class="wp-caption-text">IE9下childNodes的表现</p></div>但在IE8下，用开发人员工具查看了一下childNodes结果，发现childNodes有2个元素，即2个div元素，不包括TextNode。如下图：</p>
<p><div class="wp-caption aligncenter" style="width: 263px"><a title="IE8下childNodes的表现" href="http://witmax.cn/img/js-childnodes-ie8.png" rel="lightbox[childnodes]"><img title="IE8下childNodes的表现" src="http://witmax.cn/img/js-childnodes-ie8.png" alt="IE8下childNodes的表现" width="253" /></a><p class="wp-caption-text">IE8下childNodes的表现</p></div>至于如何处理这两种差异，只需要在Firefox中判断节点的NodeType即可，所有NodeType值如下：</p>
<table>
<tbody>
<tr>
<th>值</th>
<th>描述</th>
</tr>
<tr>
<td>1</td>
<td>Element <span>Node</span> 单元节点</td>
</tr>
<tr>
<td>2</td>
<td>Attribute <span>Node</span> 属性节点</td>
</tr>
<tr>
<td>3</td>
<td>Text <span>Node</span> 文本节点</td>
</tr>
<tr>
<td>4</td>
<td>CDATA Section <span>Node</span> CDATA选择节点</td>
</tr>
<tr>
<td>5</td>
<td>Entity Reference <span>Node</span> 实体索引节点</td>
</tr>
<tr>
<td>6</td>
<td>Entity <span>Node</span> 实体节点</td>
</tr>
<tr>
<td>7</td>
<td>Processing Instruction <span>Node</span> 进程中的指示节点</td>
</tr>
<tr>
<td>8</td>
<td>Comment <span>Node</span> 注释节点</td>
</tr>
<tr>
<td>9</td>
<td>Document <span>Node</span> 文档节点</td>
</tr>
<tr>
<td>10</td>
<td>Document Type <span>Node</span> 文档类型节点</td>
</tr>
<tr>
<td>11</td>
<td>Document Fragment <span>Node</span> 文档碎片节点</td>
</tr>
<tr>
<td>12</td>
<td>Notation <span>Node</span> 符号节点</td>
</tr>
</tbody>
</table>
<p>参考代码：</p>
<div class="runcode">
<p><textarea name="runcode" style="overflow-y:visible;width:540px;font-size:12px" class="runcode_text" id="runcode_ZY2jLL">&lt;!doctype html&gt;
&lt;html&gt;
&lt;head&gt;测试childNodes&lt;/head&gt;
&lt;body&gt;
&lt;div id=&quot;main&quot;&gt;
   &lt;div id=&quot;div1&quot;&gt;1&lt;/div&gt;
   &lt;div id=&quot;div2&quot;&gt;2&lt;/div&gt;
&lt;/div&gt;
&lt;script&gt;
 var nodes=document.getElementById(&quot;main&quot;).childNodes;
 var len = nodes.length;
 var n = 0;
 for(var i=0; i&lt;len; i++){
  if (nodes[i].nodeType == 1){
   n++;
  }
 }
 alert(len);
 alert(n);
&lt;/script&gt;
&lt;/body&gt;&lt;/html&gt;</textarea></p>
<script type="text/javascript">function changeTsize(){document.getElementById("runcode_ZY2jLL").style.height = document.getElementById("runcode_ZY2jLL").scrollHeight + "px";}window.setTimeout(changeTsize,0);</script><p><input type="button" value="运行" class="runcode_button" onclick="runcode_open_new('runcode_ZY2jLL');"/> <input type="button" value="复制" class="runcode_button" onclick="runcode_copy('runcode_ZY2jLL');"/> <input type="button" value="另存代码" class="runcode_button" onclick="saveCode('runcode_ZY2jLL','runcode_ZY2jLL');"/> 提示：你可以先修改部分代码再运行。</p>
</div>
<p>参考：<a href="https://developer.mozilla.org/cn/%E4%BB%8EInternet_Explorer%E8%BF%81%E7%A7%BB%E5%88%B0Mozilla" target="_blank">从Internet Explorer迁移到Mozilla</a></p>
]]></content:encoded>
			<wfw:commentRss>http://witmax.cn/js-childnodes.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>【Javascript】IE下substr()函数截取末尾字符时的问题</title>
		<link>http://witmax.cn/js-ie-substr.html</link>
		<comments>http://witmax.cn/js-ie-substr.html#comments</comments>
		<pubDate>Sat, 03 Sep 2011 12:14:58 +0000</pubDate>
		<dc:creator>晴枫</dc:creator>
				<category><![CDATA[网页设计]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://witmax.cn/?p=1764</guid>
		<description><![CDATA[这两天刚遇到一个问题，substr()函数在IE和FF下表现不一致，导致了一个IE下的bug。 可以通过以下代码来测试： &#60;script&#62;var str = &#34;abc&#34;; alert(str.substr(-1, 1));&#60;/script&#62; 提示：你可以先修改部分代码再运行。 希望得到的是c，FF下表现正常；但跑到IE下，显示abc，不是期望的结果。看来IE在处理substr()第一个参数为负数的情况处理方式不一样。 为了解决问题，另觅蹊径，改用substring()函数代替substr() &#60;script&#62;var str = &#34;abc&#34;; var len = str.length; alert(str.substring(len - 1, len));&#60;/script&#62; 提示：你可以先修改部分代码再运行。 p.s. 附一篇学习文章：substr() vs substring() vs slice() substr() substr() method extracts a specified number of characters in a string, from a start index. Syntax: string.substr(start, length); Quick Notes aout Substr: If start is [...]]]></description>
			<content:encoded><![CDATA[<p>这两天刚遇到一个问题，substr()函数在IE和FF下表现不一致，导致了一个IE下的bug。</p>
<p>可以通过以下代码来测试：</p>
<div class="runcode">
<p><textarea name="runcode" style="overflow-y:visible;width:540px;font-size:12px" class="runcode_text" id="runcode_i5pETs">&lt;script&gt;var str = &quot;abc&quot;;
alert(str.substr(-1, 1));&lt;/script&gt;</textarea></p>
<script type="text/javascript">function changeTsize(){document.getElementById("runcode_i5pETs").style.height = document.getElementById("runcode_i5pETs").scrollHeight + "px";}window.setTimeout(changeTsize,0);</script><p><input type="button" value="运行" class="runcode_button" onclick="runcode_open_new('runcode_i5pETs');"/> <input type="button" value="复制" class="runcode_button" onclick="runcode_copy('runcode_i5pETs');"/> <input type="button" value="另存代码" class="runcode_button" onclick="saveCode('runcode_i5pETs','runcode_i5pETs');"/> 提示：你可以先修改部分代码再运行。</p>
</div>
<p>希望得到的是c，FF下表现正常；但跑到IE下，显示abc，不是期望的结果。看来IE在处理substr()第一个参数为负数的情况处理方式不一样。</p>
<p>为了解决问题，另觅蹊径，改用substring()函数代替substr()<span id="more-1764"></span></p>
<div class="runcode">
<p><textarea name="runcode" style="overflow-y:visible;width:540px;font-size:12px" class="runcode_text" id="runcode_nVSpxl">&lt;script&gt;var str = &quot;abc&quot;;
var len = str.length;
alert(str.substring(len - 1, len));&lt;/script&gt;</textarea></p>
<script type="text/javascript">function changeTsize(){document.getElementById("runcode_nVSpxl").style.height = document.getElementById("runcode_nVSpxl").scrollHeight + "px";}window.setTimeout(changeTsize,0);</script><p><input type="button" value="运行" class="runcode_button" onclick="runcode_open_new('runcode_nVSpxl');"/> <input type="button" value="复制" class="runcode_button" onclick="runcode_copy('runcode_nVSpxl');"/> <input type="button" value="另存代码" class="runcode_button" onclick="saveCode('runcode_nVSpxl','runcode_nVSpxl');"/> 提示：你可以先修改部分代码再运行。</p>
</div>
<p>p.s. 附一篇学习文章：substr() vs substring() vs slice()</p>
<p><strong>substr()</strong></p>
<p>substr() method extracts a specified number of characters in a string, from a start index.</p>
<blockquote><p><strong>Syntax:</strong> string.substr(start, length);</p></blockquote>
<p>Quick Notes aout Substr:</p>
<ul>
<li>If start is negative, <strong>Internet Explorer</strong> returns the whole string. That’s wrong! IE should use the last character in the string.</li>
</ul>
<p><strong>substring()</strong></p>
<p>substring() method extracts the chars in a string between two specified indexes.</p>
<blockquote><p><strong>Syntax:</strong> string.substring(start, stop);</p></blockquote>
<p>Quick Notes about Substring:</p>
<ul>
<li>If start equals stop<code>, </code>it returns an empty string.</li>
<li>If stop is omitted, it extracts characters to the end of the string.</li>
<li>If either argument is less than 0 or is NaN, it is treated as if it were 0.</li>
<li>If either argument is greater than string’s length, either argument will use string’s length.</li>
<li>If start &gt; stop, then substring will swap those 2 arguments</li>
</ul>
<p><strong>slice()</strong></p>
<p>slice() works like substring() with a few different behaviors.</p>
<blockquote><p><strong>Syntax:</strong> string.slice(start, stop);</p></blockquote>
<p>Quick Notes about Slice:</p>
<ul>
<li>If stop is omitted, slice extracted chars to the end of the string, exactly like substring().</li>
<li>If start &gt; stop, slice() will NOT swap the 2 arguments.</li>
<li>If start is negative, slice() will set char from the end of string, exactly like substr() in Firefox. This behavior is observed in both Firefox and IE.</li>
<li>If stop is negative, slice() will set stop to: <em>(string.length – 1) – stop (original value).</em></li>
</ul>
<p>参考：<a href="http://rapd.wordpress.com/2007/07/12/javascript-substr-vs-substring/" target="_blank">Javascript: substr() v.s. substring()</a></p>
]]></content:encoded>
			<wfw:commentRss>http://witmax.cn/js-ie-substr.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>【Javascript】360浏览器下加入收藏夹功能无效</title>
		<link>http://witmax.cn/javascript-add-favorite.html</link>
		<comments>http://witmax.cn/javascript-add-favorite.html#comments</comments>
		<pubDate>Tue, 16 Aug 2011 17:15:31 +0000</pubDate>
		<dc:creator>晴枫</dc:creator>
				<category><![CDATA[网页设计]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://witmax.cn/?p=1739</guid>
		<description><![CDATA[很奇怪的问题，一开始以为是IE8的问题，后来查了一下网络，原来是360浏览器下加入收藏夹功能会失效。备注：测试版本为360安全浏览器3.5版。 “为了安全所以禁用了加入收藏夹功能”，360覆盖了window.external.AddFavorite方法，导致功能失效。 无解，只能给用户一个提示，引导用户“远离360”。 修改后的兼容多浏览器的加入收藏夹代码如下： 具体的原因分析见360浏览器下加入收藏夹功能失效的问题 p.s.还碰到Discuz 7在360浏览器下，发帖界面上传中文文件，文件上传后在文件列表中没有显示，暂未查到原因，如有碰到过的同学，欢迎告知。]]></description>
			<content:encoded><![CDATA[<p>很奇怪的问题，一开始以为是IE8的问题，后来查了一下网络，原来是360浏览器下加入收藏夹功能会失效。备注：测试版本为360安全浏览器3.5版。</p>
<p>“为了安全所以禁用了加入收藏夹功能”，360覆盖了window.external.AddFavorite方法，导致功能失效。</p>
<p>无解，只能给用户一个提示，引导用户“远离360”。</p>
<p>修改后的兼容多浏览器的加入收藏夹代码如下：</p>
<p><span id="more-1739"></span></p>
<pre class="brush: jscript; title: ; notranslate">function addFavorite(url, title)
{
 var ua = navigator.userAgent.toLowerCase();
 if (ua.indexOf(&quot;360se&quot;) &gt; -1){
  alert(&quot;由于360浏览器功能限制，加入收藏夹功能失效！&quot;);
 }
 else if (ua.indexOf(&quot;msie 8&quot;) &gt; -1){
  window.external.AddToFavoritesBar(url, title);//IE8
 }
 else if (document.all){
  window.external.addFavorite(url, title);
 }
 else if (window.sidebar){
  window.sidebar.addPanel(title, url, &quot;&quot;);
 }
 else{
  alert(&quot;对不起，您的浏览器不支持加入收藏夹功能！&quot;);
 }
} </pre>
<p>具体的原因分析见<a href="http://hi.baidu.com/yanzuoguang/blog/item/0fda300e08b82bfdab6457ae.html" target="_blank">360浏览器下加入收藏夹功能失效的问题</a></p>
<p>p.s.还碰到Discuz 7在360浏览器下，发帖界面上传中文文件，文件上传后在文件列表中没有显示，暂未查到原因，如有碰到过的同学，欢迎告知。</p>
]]></content:encoded>
			<wfw:commentRss>http://witmax.cn/javascript-add-favorite.html/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Web前端开发笔试题集锦（Javascript篇）</title>
		<link>http://witmax.cn/web-dev-test-js.html</link>
		<comments>http://witmax.cn/web-dev-test-js.html#comments</comments>
		<pubDate>Sun, 08 May 2011 11:47:36 +0000</pubDate>
		<dc:creator>晴枫</dc:creator>
				<category><![CDATA[网页设计]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://witmax.cn/?p=1588</guid>
		<description><![CDATA[以下为Web前端开发笔试题集锦之Javascript篇，移步HTML/CSS篇 1, 判断字符串是否是这样组成的，第一个必须是字母，后面可以是字母、数字、下划线，总长度为5-20 2，截取字符串abcdefg的efg 3，判断一个字符串中出现次数最多的字符，统计这个次数 4，IE与FF脚本兼容性问题 (1) window.event： 表示当前的事件对象，IE有这个对象，FF没有，FF通过给事件处理函数传递事件对象 (2) 获取事件源 IE用srcElement获取事件源，而FF用target获取事件源 (3) 添加，去除事件 IE：element.attachEvent(“onclick”, function) element.detachEvent(“onclick”, function) FF：element.addEventListener(“click”, function, true) element.removeEventListener(“click”, function, true) (4) 获取标签的自定义属性 IE：div1.value或div1[“value”] FF：可用div1.getAttribute(“value”) (5) document.getElementsByName()和document.all[name] IE；document.getElementsByName()和document.all[name]均不能获取div元素 FF：可以 (6) input.type的属性 IE：input.type只读 FF：input.type可读写 (7) innerText textContent outerHTML IE：支持innerText, outerHTML FF：支持textContent (8) 是否可用id代替HTML元素 IE：可以用id来代替HTML元素 FF：不可以 这里只列出了常见的，还有不少，更多的介绍可以参看JavaScript在IE浏览器和Firefox浏览器中的差异总结 5，规避javascript多人开发函数重名问题 (1) 可以开发前规定命名规范，根据不同开发人员开发的功能在函数前加前缀 (2) 将每个开发人员的函数封装到类中，调用的时候就调用类的函数，即使函数重名只要类名不重复就ok 6，javascript面向对象中继承实现 javascript面向对象中的继承实现一般都使用到了构造函数和Prototype原型链，简单的代码如下： [...]]]></description>
			<content:encoded><![CDATA[<p>以下为Web前端开发笔试题集锦之Javascript篇，移步<a title="Web前端开发笔试题集锦（HTML/CSS篇）" href="http://witmax.cn/web-dev-test-html.html">HTML/CSS篇</a></p>
<p><strong>1, 判断字符串是否是这样组成的，第一个必须是字母，后面可以是字母、数字、下划线，总长度为5-20</strong></p>
<pre class="brush: jscript; title: ; notranslate">var reg = /^[a-zA-Z][a-zA-Z_0-9]{4,19}$/;
reg.test(&quot;a1a__a1a__a1a__a1a__&quot;);</pre>
<p><strong>2，截取字符串abcdefg的efg</strong></p>
<pre class="brush: jscript; title: ; notranslate">var str = &quot;abcdefg&quot;;
if (/efg/.test(str)) {
  var efg = str.substr(str.indexOf(&quot;efg&quot;), 3);
  alert(efg);
}</pre>
<p><strong>3，判断一个字符串中出现次数最多的字符，统计这个次数</strong></p>
<p><span id="more-1588"></span></p>
<pre class="brush: jscript; title: ; notranslate">//将字符串的字符保存在一个hash table中，key是字符，value是这个字符出现的次数
var str = &quot;abcdefgaddda&quot;;
var obj = {};
for (var i = 0, l = str.length; i &amp;lt; l; i++) {
   var key = str[i];
   if (!obj[key]) {
      obj[key] = 1;
   } else {
      obj[key]++;
   }
}

/*遍历这个hash table，获取value最大的key和value*/
var max = -1;
var max_key = &quot;&quot;;
var key;
for (key in obj) {
   if (max &lt; obj[key]) {
      max = obj[key];
      max_key = key;
   }
}

alert(&quot;max:&quot;+max+&quot; max_key:&quot;+max_key);</pre>
<p><strong>4，IE与FF脚本兼容性问题</strong></p>
<p>(1) window.event：</p>
<p>表示当前的事件对象，IE有这个对象，FF没有，FF通过给事件处理函数传递事件对象</p>
<p>(2) 获取事件源</p>
<p>IE用srcElement获取事件源，而FF用target获取事件源</p>
<p>(3) 添加，去除事件</p>
<p>IE：element.attachEvent(“onclick”, function) element.detachEvent(“onclick”, function)</p>
<p>FF：element.addEventListener(“click”, function, true) element.removeEventListener(“click”, function, true)</p>
<p>(4) 获取标签的自定义属性</p>
<p>IE：div1.value或div1[“value”]</p>
<p>FF：可用div1.getAttribute(“value”)</p>
<p>(5) document.getElementsByName()和document.all[name]</p>
<p>IE；document.getElementsByName()和document.all[name]均不能获取div元素</p>
<p>FF：可以</p>
<p>(6) input.type的属性</p>
<p>IE：input.type只读</p>
<p>FF：input.type可读写</p>
<p>(7) innerText textContent outerHTML</p>
<p>IE：支持innerText, outerHTML</p>
<p>FF：支持textContent</p>
<p>(8) 是否可用id代替HTML元素</p>
<p>IE：可以用id来代替HTML元素</p>
<p>FF：不可以</p>
<p>这里只列出了常见的，还有不少，更多的介绍可以参看<a title="JavaScript在IE浏览器和Firefox浏览器中的差异总结" href="http://witmax.cn/javascript-difference-summary.html" target="_blank">JavaScript在IE浏览器和Firefox浏览器中的差异总结</a></p>
<p><strong>5，规避javascript多人开发函数重名问题</strong></p>
<p>(1) 可以开发前规定命名规范，根据不同开发人员开发的功能在函数前加前缀</p>
<p>(2) 将每个开发人员的函数封装到类中，调用的时候就调用类的函数，即使函数重名只要类名不重复就ok</p>
<p><strong>6，javascript面向对象中继承实现</strong></p>
<p>javascript面向对象中的继承实现一般都使用到了构造函数和Prototype原型链，简单的代码如下：</p>
<pre class="brush: jscript; title: ; notranslate">function Animal(name) {
   this.name = name;
}

Animal.prototype.getName = function() {alert(this.name)}
function Dog() {};
Dog.prototype = new Animal(&quot;Buddy&quot;);
Dog.prototype.constructor = Dog;
var dog = new Dog();</pre>
<p><strong>7，FF下面实现outerHTML</strong></p>
<p>FF不支持outerHTML，要实现outerHTML还需要特殊处理</p>
<p>思路如下：</p>
<p>在页面中添加一个新的元素A，克隆一份需要获取outerHTML的元素，将这个元素append到新的A中，然后获取A的innerHTML就可以了。</p>
<pre class="brush: xml; title: ; notranslate"> &lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt; 
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt; 
&lt;head&gt; 
&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=gb2312&quot; /&gt; 
&lt;title&gt;获取outerHMTL&lt;/title&gt; 
&lt;style&gt; 
div{ background:#0000FF;width:100px;height:100px;}  
span{ background:#00FF00;width:100px;height:100px;}  
p{ background:#FF0000;width:100px;height:100px;}  
&lt;/style&gt; 
&lt;/head&gt; 
&lt;body&gt; 
&lt;div id=&quot;a&quot;&gt;&lt;span&gt;SPAN&lt;/span&gt;DIV&lt;/div&gt; 
&lt;span&gt;SPAN&lt;/span&gt;
&lt;p&gt;P&lt;/p&gt; 
&lt;script type=&quot;text/javascript&quot;&gt;
function getOuterHTML(id){
 var el = document.getElementById(id);
 var newNode = document.createElement(&quot;div&quot;);
 document.appendChild(newNode);
 var clone = el.cloneNode(true);
 newNode.appendChild(clone);
 alert(newNode.innerHTML);
 document.removeChild(newNode);
}
getOuterHTML(&quot;a&quot;);
&lt;/script&gt;
&lt;/body&gt; 
&lt;/html&gt;</pre>
<p><strong>8，编写一个方法 求一个字符串的字节长度</strong></p>
<p>假设：</p>
<p>一个英文字符占用一个字节，一个中文字符占用两个字节</p>
<pre class="brush: jscript; title: ; notranslate">function GetBytes(str){
    var len = str.length;
    var bytes = len;
    for(var i=0; i&lt;len; i++){
        if (str.charCodeAt(i) &gt; 255) bytes++;
    }
    return bytes;
}
alert(GetBytes(&quot;你好,as&quot;));</pre>
<p><strong>9，编写一个方法 去掉一个数组的重复元素</strong></p>
<pre class="brush: jscript; title: ; notranslate">var arr = [1 ,1 ,2, 3, 3, 2, 1];
Array.prototype.unique = function(){
    var ret = [];
    var o = {};
    var len = this.length;
    for (var i=0; i&lt;len; i++){
        var v = this[i];
        if (!o[v]){
            o[v] = 1;
            ret.push(v);
        }
    }
    return ret;
};
alert(arr.unique());</pre>
<p><strong>10，写出3个使用this的典型应用</strong></p>
<p>（1）在html元素事件属性中使用，如</p>
<pre class="brush: xml; title: ; notranslate">&lt;input type=”button” onclick=”showInfo(this);” value=”点击一下”/&gt;</pre>
<p>（2）构造函数</p>
<pre class="brush: jscript; title: ; notranslate">function Animal(name, color) {
   this.name = name;
   this.color = color;
}</pre>
<p>（3）</p>
<pre class="brush: xml; title: ; notranslate">&lt;input type=&quot;button&quot; id=&quot;text&quot; value=&quot;点击一下&quot; /&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
var btn = document.getElementById(&quot;text&quot;);
btn.onclick = function() {
alert(this.value); //此处的this是按钮元素
}
&lt;/script&gt;</pre>
<p>（4）CSS expression表达式中使用this关键字</p>
<pre class="brush: xml; title: ; notranslate">&lt;table width=&quot;100px&quot; height=&quot;100px&quot;&gt;
  &lt;tr&gt;
    &lt;td&gt;
    &lt;div style=&quot;width:expression(this.parentNode.width);&quot;&gt;div element&lt;/div&gt;
    &lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;</pre>
<p><strong>12，如何显示/隐藏一个DOM元素？</strong></p>
<pre class="brush: jscript; title: ; notranslate">el.style.display = &quot;&quot;;
el.style.display = &quot;none&quot;;</pre>
<p>el是要操作的DOM元素</p>
<p><strong>13，JavaScript中如何检测一个变量是一个String类型？请写出函数实现</strong></p>
<p><strong>String类型有两种生成方式：</strong></p>
<p>(1)Var str = “hello world”;</p>
<p>(2)Var str2 = new String(“hello world”);</p>
<pre class="brush: jscript; title: ; notranslate">function IsString(str){
    return (typeof str == &quot;string&quot; || str.constructor == String);
}
var str = &quot;&quot;;
alert(IsString(1));
alert(IsString(str));
alert(IsString(new String(str)));</pre>
<p><strong>14，网页中实现一个计算当年还剩多少时间的倒数计时程序，要求网页上实时动态显示“××年还剩××天××时××分××秒” </strong></p>
<pre class="brush: xml; title: ; notranslate">&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot; &quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;
&lt;html&gt;
&lt;head&gt;
   &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot;&gt;
   &lt;title&gt;倒计时&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;input type=&quot;text&quot; value=&quot;&quot; id=&quot;input&quot; size=&quot;1000&quot;/&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
   function counter() {
      var date = new Date();
      var year = date.getFullYear();
      var date2 = new Date(year, 12, 31, 23, 59, 59);
      var time = (date2 - date)/1000;
      var day =Math.floor(time/(24*60*60))
      var hour = Math.floor(time%(24*60*60)/(60*60))
      var minute = Math.floor(time%(24*60*60)%(60*60)/60);
      var second = Math.floor(time%(24*60*60)%(60*60)%60);
      var str = year + &quot;年还剩&quot;+day+&quot;天&quot;+hour+&quot;时&quot;+minute+&quot;分&quot;+second+&quot;秒&quot;;
      document.getElementById(&quot;input&quot;).value = str;
   }
   window.setInterval(&quot;counter()&quot;, 1000);
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;</pre>
<p><strong>15，补充代码，鼠标单击Button1后将Button1移动到Button2的后面&lt;div&gt; &lt;input type=”button” id =”button1&#8243; value=”1&#8243; onclick=”???”&gt; &lt;input type=”button” id =”button2&#8243; value=”2&#8243; /”&gt; &lt;/div&gt;</strong></p>
<pre class="brush: xml; title: ; notranslate">&lt;div&gt;
   &lt;input type=&quot;button&quot; id =&quot;button1&quot; value=&quot;1&quot; onclick=&quot;moveBtn(this);&quot;&gt;
   &lt;input type=&quot;button&quot; id =&quot;button2&quot; value=&quot;2&quot; /&gt;
&lt;/div&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
function moveBtn(obj) {
   var clone = obj.cloneNode(true);
   var parent = obj.parentNode;
   parent.appendChild(clone);
   parent.removeChild(obj);
}
&lt;/script&gt;</pre>
<p><strong>16，JavaScript有哪几种数据类型</strong></p>
<p>简单：Number，Boolean，String，Null，Undefined</p>
<p>复合：Object，Array，Function</p>
<p><strong>17，下面css标签在JavaScript中调用应如何拼写，border-left-color，-moz-viewport</strong></p>
<p>borderLeftColor</p>
<p>mozViewport</p>
<p><strong>18，JavaScript中如何对一个对象进行深度clone</strong></p>
<pre class="brush: jscript; title: ; notranslate">function cloneObject(o) {
    if(!o || 'object' !== typeof o) {
        return o;
    }
    var c = 'function' === typeof o.pop ? [] : {};
    var p, v;
    for(p in o) {
        if(o.hasOwnProperty(p)) {
            v = o[p];
            if(v &amp;&amp; 'object' === typeof v) {
                c[p] = Ext.ux.clone(v);
            }
            else {
                c[p] = v;
            }
        }
    }
    return c;
}; </pre>
<p><strong>19，如何控制alert中的换行</strong></p>
<p>\n alert(“p\np”);</p>
<p><strong>20，请实现，鼠标点击页面中的任意标签，alert该标签的名称．（注意兼容性）</strong></p>
<pre class="brush: xml; title: ; notranslate">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt; 
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt; 
&lt;head&gt; 
&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=gb2312&quot; /&gt; 
&lt;title&gt;鼠标点击页面中的任意标签，alert该标签的名称&lt;/title&gt; 
&lt;style&gt; 
div{ background:#0000FF;width:100px;height:100px;}  
span{ background:#00FF00;width:100px;height:100px;}  
p{ background:#FF0000;width:100px;height:100px;}  
&lt;/style&gt; 
&lt;script type=&quot;text/javascript&quot;&gt;
document.onclick = function(evt){
 var e = window.event || evt;
 var tag = e[&quot;target&quot;] || e[&quot;srcElement&quot;];
 alert(tag.tagName);
};
&lt;/script&gt;
&lt;/head&gt; 
&lt;body&gt; 
&lt;div id=&quot;div&quot;&gt;&lt;span&gt;SPAN&lt;/span&gt;DIV&lt;/div&gt; 
&lt;span&gt;SPAN&lt;/span&gt;
&lt;p&gt;P&lt;/p&gt; 
&lt;/body&gt; 
&lt;/html&gt; </pre>
<p><strong>21，请编写一个JavaScript函数 parseQueryString，它的用途是把URL参数解析为一个对象，如：</strong></p>
<p><strong>var url = “http://witmax.cn/index.php?key0=0&amp;key1=1&amp;key2=2&#8243;;</strong></p>
<pre class="brush: jscript; title: ; notranslate">

function parseQueryString(url){
   var params = {};
   var arr = url.split(&quot;?&quot;);
   if (arr.length &lt;= 1)
      return params;
   arr = arr[1].split(&quot;&amp;&quot;);
   for(var i=0, l=arr.length; i&lt;l; i++){
      var a = arr[i].split(&quot;=&quot;);
      params[a[0]] = a[1];
   }
   return params;
}

var url = &quot;http://witmax.cn/index.php?key0=0&amp;key1=1&amp;key2=2&quot;;
var ps = parseQueryString(url);
alert(ps[&quot;key1&quot;]);</pre>
<p><strong>22，ajax是什么? ajax的交互模型? 同步和异步的区别? 如何解决跨域问题?</strong></p>
<p>Ajax是多种技术组合起来的一种浏览器和服务器交互技术，基本思想是允许一个互联网浏览器向一个远程页面/服务做异步的http调用，并且用收到的数据来更新一个当前web页面而不必刷新整个页面。该技术能够改进客户端的体验。包含的技术：</p>
<p>XHTML：对应W3C的XHTML规范，目前是XHTML1.0。</p>
<p>CSS：对应W3C的CSS规范，目前是CSS2.0</p>
<p>DOM：这里的DOM主要是指HTML DOM，XML DOM包括在下面的XML中</p>
<p>JavaScript：对应于ECMA的ECMAScript规范</p>
<p>XML：对应W3C的XML DOM、XSLT、XPath等等规范</p>
<p>XMLHttpRequest：对应WhatWG的Web Applications1.0规范（<a href="http://whatwg.org/specs/web-apps/current-work/">http://whatwg.org/specs/web-apps/current-work/</a>）</p>
<p>AJAX交互模型</p>
<p><img src="http://www.xuchen.name/media/agh4dWNoZW5oaXINCxIFTWVkaWEY2eIbDA/ajax.GIF" alt="" /></p>
<p>同步：脚本会停留并等待服务器发送回复然后再继续</p>
<p>异步：脚本允许页面继续其<a href="http://whatis.ctocio.com.cn/searchwhatis/292/7333792.shtml">进程</a>并处理可能的回复</p>
<p>跨域问题简单的理解就是因为JS同源策略的限制，a.com域名下的JS无法操作b.com或c.a.com下的对象，具体场景如下：</p>
<p><img src="http://www.xuchen.name/media/agh4dWNoZW5oaXINCxIFTWVkaWEYweobDA/kuayu.GIF" alt="" /></p>
<p>PS：（1）如果是端口或者协议造成的跨域问题前端是无能为力的</p>
<p>(2) 在跨域问题上，域仅仅通过URL的首部来识别而不会尝试判断相同的IP地址对应的域或者两个域是否对应一个IP</p>
<p>前端对于跨域的解决办法：</p>
<p>(1) document.domain+iframe</p>
<p>(2) 动态创建script标签</p>
<p><strong>23，什么是闭包？下面这个ul，如何点击每一列的时候alert其index?</strong></p>
<pre class="brush: xml; title: ; notranslate">&lt;ul id=”test”&gt;
&lt;li&gt;这是第一条&lt;/li&gt;
&lt;li&gt;这是第二条&lt;/li&gt;
&lt;li&gt;这是第三条&lt;/li&gt;
&lt;/ul&gt;</pre>
<p>内部函数被定义它的函数的外部区域调用的时候就产生了闭包。</p>
<pre class="brush: jscript; title: ; notranslate">(function A() {
   var index = 0;
   var ul = document.getElementById(&quot;test&quot;);
   var obj = {};
   for (var i = 0, l = ul.childNodes.length; i &lt; l; i++) {
      if (ul.childNodes[i].nodeName.toLowerCase() == &quot;li&quot;) {
         var li = ul.childNodes[i];
         li.onclick = function() {
            index++;
            alert(index);
         }
      }
   }
})();</pre>
<p><strong>24，请给出异步加载js方案，不少于两种</strong></p>
<p>默认情况javascript是同步加载的，也就是javascript的加载时阻塞的，后面的元素要等待javascript加载完毕后才能进行再加载，对于一些意义不是很大的javascript，如果放在页头会导致加载很慢的话，是会严重影响用户体验的。</p>
<p>异步加载方式：</p>
<p>(1) defer，只支持IE</p>
<p>(2) async：</p>
<p>(3) 创建script，插入到DOM中，加载完毕后callBack，见代码：</p>
<pre class="brush: jscript; title: ; notranslate">function loadScript(url, callback){
   var script = document.createElement(&quot;script&quot;)
   script.type = &quot;text/javascript&quot;;
   if (script.readyState){ //IE
      script.onreadystatechange = function(){
         if (script.readyState == &quot;loaded&quot; ||
            script.readyState == &quot;complete&quot;){
            script.onreadystatechange = null;
            callback();
         }
      };
   } else { //Others: Firefox, Safari, Chrome, and Opera
      script.onload = function(){
          callback();
      };
   }
   script.src = url;
   document.body.appendChild(script);
}</pre>
<p><strong>25，请设计一套方案，用于确保页面中JS加载完全。</strong></p>
<pre class="brush: jscript; title: ; notranslate">var n = document.createElement(&quot;script&quot;);
n.type = &quot;text/javascript&quot;;
//以上省略部分代码
//ie支持script的readystatechange属性(IE support the readystatechange event for script and css nodes)
if(ua.ie){
   n.onreadystatechange = function(){
       var rs = this.readyState;
       if('loaded' === rs || 'complete'===rs){
           n.onreadystatechange = null;
           f(id,url); //回调函数
       }
};
//省略部分代码
//safari 3.x supports the load event for script nodes(DOM2)
   n.addEventListener('load',function(){
       f(id,url);
   });
//firefox and opera support onload(but not dom2 in ff) handlers for
//script nodes. opera, but no ff, support the onload event for link
//nodes.
}else{
   n.onload = function(){
       f(id,url);
   };
}</pre>
<p><strong>26，js中如何定义class,如何扩展prototype?</strong></p>
<p>Ele.className = “***”; //***在css中定义，形式如下：.*** {…}</p>
<p>A.prototype.B = C;</p>
<p>A是某个构造函数的名字</p>
<p>B是这个构造函数的属性</p>
<p>C是想要定义的属性的值</p>
<p><strong>27，如何添加html元素的事件,有几种方法.</strong></p>
<p>（1） 为HTML元素的事件属性赋值</p>
<p>（2） 在JS中使用ele.on*** = function() {…}</p>
<p>（3） 使用DOM2的添加事件的方法 addEventListener或attachEvent</p>
<p><strong>28，documen.write和 innerHTML的区别</strong></p>
<p>document.write只能重绘整个页面</p>
<p>innerHTML可以重绘页面的一部分</p>
<p><strong>29，多浏览器检测通过什么？</strong></p>
<p>（1） navigator.userAgent</p>
<p>（2） 不同浏览器的特性，如addEventListener</p>
<p><strong>30，js的基础对象有那些, window和document的常用的方法和属性列出来</strong></p>
<p>String,Number,Boolean</p>
<p>Window:</p>
<p>方法：setInterval,setTimeout,clearInterval,clearTimeout,alert,confirm,open</p>
<p>属性：name,parent,screenLeft,screenTop,self,top,status</p>
<p>Document</p>
<p>方法：createElement,execCommand,getElementById,getElementsByName,getElementByTagName,write,writeln</p>
<p>属性：cookie,doctype,domain,documentElement,readyState,URL,</p>
<p><strong>31，前端开发的优化问题</strong></p>
<p>（1） 减少http请求次数：css spirit,data uri</p>
<p>（2） JS，CSS源码压缩</p>
<p>（3） 前端模板 JS+数据，减少由于HTML标签导致的带宽浪费，前端用变量保存AJAX请求结果，每次操作本地变量，不用请求，减少请求次数</p>
<p>（4） 用innerHTML代替DOM操作，减少DOM操作次数，优化javascript性能</p>
<p>（5） 用setTimeout来避免页面失去响应</p>
<p>（6） 用hash-table来优化查找</p>
<p>（7） 当需要设置的样式很多时设置className而不是直接操作style</p>
<p>（8） 少用全局变量</p>
<p>（9） 缓存DOM节点查找的结果</p>
<p>（10） 避免使用CSS Expression</p>
<p>（11） 图片预载</p>
<p>（12） 避免在页面的主体布局中使用table，table要等其中的内容完全下载之后才会显示出来，显示比div+css布局慢</p>
<p><strong>32，如何控制网页在网络传输过程中的数据量</strong></p>
<p>启用GZIP压缩</p>
<p>保持良好的编程习惯，避免重复的CSS，JavaScript代码，多余的HTML标签和属性</p>
<p><strong>33，Flash、Ajax各自的优缺点，在使用中如何取舍？</strong></p>
<p>Ajax的优势</p>
<p>（1） 可搜索型</p>
<p>（2） 开放性</p>
<p>（3） 费用</p>
<p>（4） 易用性</p>
<p>（5） 易于开发</p>
<p>Flash的优势</p>
<p>（1） 多媒体处理</p>
<p>（2） 兼容性</p>
<p>（3） 矢量图形 比SVG，Canvas优势大很多</p>
<p>（4） 客户端资源调度，比如麦克风，摄像头</p>
<p>参考：<a href="http://www.xuchen.name/?p=440002">http://www.xuchen.name/?p=440002</a> （修改了部分答案）</p>
]]></content:encoded>
			<wfw:commentRss>http://witmax.cn/web-dev-test-js.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>JavaScript在IE浏览器和Firefox浏览器中的差异总结</title>
		<link>http://witmax.cn/javascript-difference-summary.html</link>
		<comments>http://witmax.cn/javascript-difference-summary.html#comments</comments>
		<pubDate>Fri, 06 May 2011 12:09:51 +0000</pubDate>
		<dc:creator>晴枫</dc:creator>
				<category><![CDATA[网页设计]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://witmax.cn/?p=1568</guid>
		<description><![CDATA[JavaScript在IE浏览器和Firefox浏览器中存在一些差异，以下对这些差异部分进行了总结，以及解决方案： 1.HTML对象的 id 作为对象名的问题 IE：HTML 对象的 ID 可以作为 document 的下属对象变量名直接使用 Firefox：不支持 解决方法：使用document.getElementById替代document.all 2.如果控件只有name,没有id, 用getElementById时: IE：可以找到对象 Firefox：返回NULL 解决方法：所有控件必须设置ID属性 3.Evel: IE：支持，利用 eval(idName) 可以取得 id 为 idName 的 HTML 对象 Firefox：不支持 解决方法：统一使用getElementById(idName) 代替 eval(idName)。 4.Event： IE：用全局对象window.event Firefox：event只能在事件发生的现场使用，在调用函数时传入event对象 解决方法：使用之前做一下浏览器类型判断，各用各的。 5.对象名称中美元符号‘$’ 改为使用下划线‘_’ 6.集合类对象问题 IE：可以使用()或[]获取集合类对象 Firefox：只能使用[]获取集合类对象 现有代码中存在许多，不能在 Firefox 下运行 解决方法:统一使用[]获取集合类对象。document.form.item(“itemName”) 这样的语句改为document.form.elements["elementName"] 7.变量名与某 HTML 对象 id 相同的问题 IE：不能使用与 HTML 对象 id 相同的变量名 [...]]]></description>
			<content:encoded><![CDATA[<p>JavaScript在IE浏览器和Firefox浏览器中存在一些差异，以下对这些差异部分进行了总结，以及解决方案：</p>
<p>1.HTML对象的 id 作为对象名的问题</p>
<ul>
<li>IE：HTML 对象的 ID 可以作为 document 的下属对象变量名直接使用</li>
<li>Firefox：不支持</li>
</ul>
<p>解决方法：使用document.getElementById替代document.all</p>
<p>2.如果控件只有name,没有id, 用getElementById时:</p>
<p><span id="more-1568"></span></p>
<ul>
<li>IE：可以找到对象</li>
<li>Firefox：返回NULL</li>
</ul>
<p>解决方法：所有控件必须设置ID属性</p>
<p>3.Evel:</p>
<ul>
<li>IE：支持，利用 eval(idName) 可以取得 id 为 idName 的 HTML 对象</li>
<li>Firefox：不支持</li>
</ul>
<p>解决方法：统一使用getElementById(idName) 代替 eval(idName)。</p>
<p>4.Event：</p>
<ul>
<li>IE：用全局对象window.event</li>
<li>Firefox：event只能在事件发生的现场使用，在调用函数时传入event对象</li>
</ul>
<p>解决方法：使用之前做一下浏览器类型判断，各用各的。</p>
<p>5.对象名称中美元符号‘$’ 改为使用下划线‘_’</p>
<p>6.集合类对象问题</p>
<ul>
<li>IE：可以使用()或[]获取集合类对象</li>
<li>Firefox：只能使用[]获取集合类对象</li>
</ul>
<p>现有代码中存在许多，不能在 Firefox 下运行</p>
<p>解决方法:统一使用[]获取集合类对象。document.form.item(“itemName”) 这样的语句改为document.form.elements["elementName"]</p>
<p>7.变量名与某 HTML 对象 id 相同的问题</p>
<ul>
<li>IE：不能使用与 HTML 对象 id 相同的变量名</li>
<li>Firefox：可以使用</li>
</ul>
<p>解决方法：在声明变量时，一律加上 var ，以避免歧义，这样在 IE 中亦可正常运行。此外，最好不要取与 HTML 对象 id 相同的变量名，以减少错误。</p>
<p>8.Event定位问题</p>
<ul>
<li>IE：支持event.x 和event.y</li>
<li>Firefox：支持event.pageX和event.pageY</li>
</ul>
<p>解决方法，统一使用event.clientX和event.clientY，但是在Firefox中event.clientX 与 event.pageX 有微妙的差别（当整个页面有滚动条的时候），不过大多数时候是等效的。如果要完全一样，可以判断浏览器类型后对应使用。</p>
<p>9.父结点的问题</p>
<ul>
<li>IE：parentElement parentElement.children</li>
<li>Firefox：parentNode parentNode.childNodes</li>
</ul>
<p>childNodes的下标的含义在IE和Firefox中不同，Firefox使用DOM规范，childNodes中会插入空白文本节点。一般可以通过node.getElementsByTagName()来回避这个问题。</p>
<p>当html中节点缺失时，IE和Firefox对parentNode的解释不同。例如:</p>
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<pre>&lt;form&gt;
	&lt;table&gt;
		&lt;input/&gt;
	&lt;/table&gt;
&lt;/form&gt;</pre>
</tr>
</tbody>
</table>
<ul>
<li>IE：input.parentNode的值为空节点</li>
<li>Firefox：input.parentNode的值为form</li>
</ul>
<p>解决方法：Firefox中节点没有removeNode方法，必须使用如下方法 node.parentNode.removeChild(node)</p>
<p>10.const 问题</p>
<ul>
<li>IE：不支持 const 关键字。如 const constVar = 32; 在IE中这是语法错误</li>
<li>Firefox：支持</li>
</ul>
<p>解决方法：不使用 const ，以 var 代替。</p>
<p>11.body 对象</p>
<ul>
<li>IE：在body标签完全被读入之后才存在</li>
<li>Firefox：在body标签没有被浏览器完全读入之前就存在</li>
</ul>
<p>12.自定义属性问题</p>
<ul>
<li>IE：可以使用获取常规属性的方法来获取自定义属性,也可以使用getAttribute()获取自定义属性</li>
<li>Firefox：只能使用getAttribute()获取自定义属性.</li>
</ul>
<p>解决方法：统一通过getAttribute()获取自定义属性</p>
<p>13.event.srcElement问题</p>
<ul>
<li>IE：even.srcElement</li>
<li>Firefox：even.target</li>
</ul>
<p>解决方法:使用时判断浏览器类型，各用各的。</p>
<p>14.模态和非模态窗口</p>
<ul>
<li>IE：支持模态和非模态窗口</li>
<li>Firefox：不支持</li>
</ul>
<p>解决方法：直接使用window.open(pageURL,name,parameters)方式打开新窗口。如果需要将子窗口中的参数传递回父窗口,可以在子窗口中使用window.opener来访问父窗口。</p>
<p>15.innerText</p>
<ul>
<li>IE：innerText</li>
<li>Firefox：textContent</li>
</ul>
<p>16.类似 obj.style.height = imgObj.height 的语句</p>
<ul>
<li>IE：有效</li>
<li>Firefox：无效</li>
</ul>
<p>解决方法：统一使用obj.style.height = imgObj.height + &#8216;px&#8217;;</p>
<p>原文：<a href="http://express.ruanko.com/ruanko-express_34/tech-overnight2.html">http://express.ruanko.com/ruanko-express_34/tech-overnight2.html</a></p>
]]></content:encoded>
			<wfw:commentRss>http://witmax.cn/javascript-difference-summary.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>JSON格式与XML格式的优劣比较</title>
		<link>http://witmax.cn/json-vs-xml.html</link>
		<comments>http://witmax.cn/json-vs-xml.html#comments</comments>
		<pubDate>Sat, 23 Apr 2011 14:50:15 +0000</pubDate>
		<dc:creator>晴枫</dc:creator>
				<category><![CDATA[网页设计]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://witmax.cn/?p=1539</guid>
		<description><![CDATA[JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。易于人阅读和编写，同时也易于机器解析和生成。它基于JavaScript（Standard ECMA-262 3rd Edition &#8211; December 1999）的一个子集。 JSON采用完全独立于语言的文本格式，但是也使用了类似于C语言家族的习惯（包括C, C++, C#, Java, JavaScript, Perl, Python等）。这些特性使JSON成为理想的数据交换语言。具体的表示格式见官方说明。 XML（Extensible Markup Language）即可扩展标记语言，它与HTML一样，都是SGML(Standard Generalized Markup Language,标准通用标记语言)。Xml是Internet环境中跨平台的，依赖于内容的技术，是当前处理结构化文档信息的有力工具。扩展标记语言XML是一种简单的数据存储语言，使用一系列简单的标记描述数据，而这些标记可以用方便的方式建立，虽然XML占用的空间比二进制数据要占用更多的空间，但XML极其简单易于掌握和使用。 两者的共同优点是都是文本表示的数据格式，可以跨平台、跨系统交换数据。 就可读性和可扩展性而言，JSON和XML不相上下。 就编码难度而言，JSON略比XML简单，要写好XML最好还是借助XML编辑器，标签的嵌套和配对手动输入来说容易出错。因此，从代码的可维护性角度而言，JSON优于XML。 就内容解析而言，JSON在客户端只要使用eval()便可完成解析，在服务器端有包括ActionScript, C, C#, ColdFusion, Java, JavaScript, Perl, PHP, Python, Ruby等语言的解析支持，非常简单；而XML则相对复杂，不管在服务器端还是在客户端都需要花费大量的代码来实现解析。具体实例见JSON 与 XML 的比较。 就数据表示和传输性能而言，JSON明显比XML简洁，格式简单，占用带宽少。 就安全性而言，由于大部分Javascript库都是使用eval()来解析数据，存在执行恶意JSON数据的安全漏洞；当然可以使用专门的JSON解析器来避免这个问题。相比，XML更安全一些。 参考资料： http://www.json.org/json-zh.html JSON 与 XML 的比较]]></description>
			<content:encoded><![CDATA[<p>JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。易于人阅读和编写，同时也易于机器解析和生成。它基于JavaScript（Standard ECMA-262 3rd Edition &#8211; December 1999）的一个子集。 JSON采用完全独立于语言的文本格式，但是也使用了类似于C语言家族的习惯（包括C, C++, C#, Java, JavaScript, Perl, Python等）。这些特性使JSON成为理想的数据交换语言。具体的表示格式见<a href="http://www.json.org/json-zh.html">官方说明</a>。</p>
<p>XML（Extensible Markup Language）即可扩展标记语言，它与HTML一样，都是SGML(Standard Generalized Markup Language,标准通用标记语言)。Xml是Internet环境中跨平台的，依赖于内容的技术，是当前处理结构化文档信息的有力工具。扩展标记语言XML是一种简单的数据存储语言，使用一系列简单的标记描述数据，而这些标记可以用方便的方式建立，虽然XML占用的空间比二进制数据要占用更多的空间，但XML极其简单易于掌握和使用。</p>
<p>两者的<strong>共同优点</strong>是都是文本表示的数据格式，可以跨平台、跨系统交换数据。</p>
<p><span id="more-1539"></span></p>
<p>就<strong>可读性和可扩展性</strong>而言，JSON和XML不相上下。</p>
<p>就<strong>编码难度</strong>而言，JSON略比XML简单，要写好XML最好还是借助XML编辑器，标签的嵌套和配对手动输入来说容易出错。因此，从代码的可维护性角度而言，JSON优于XML。</p>
<p>就<strong>内容解析</strong>而言，JSON在客户端只要使用eval()便可完成解析，在服务器端有包括ActionScript, C, C#, ColdFusion, Java, JavaScript, Perl, PHP, Python, Ruby等语言的解析支持，非常简单；而XML则相对复杂，不管在服务器端还是在客户端都需要花费大量的代码来实现解析。具体实例见<a href="http://www.nowamagic.net/javascript/js_JsonVSXML.php">JSON 与 XML 的比较</a>。</p>
<p>就<strong>数据表示和传输性能</strong>而言，JSON明显比XML简洁，格式简单，占用带宽少。</p>
<p>就<strong>安全性</strong>而言，由于大部分Javascript库都是使用eval()来解析数据，存在执行恶意JSON数据的安全漏洞；当然可以使用专门的JSON解析器来避免这个问题。相比，XML更安全一些。</p>
<p>参考资料：</p>
<ol>
<li><a href="http://www.json.org/json-zh.html">http://www.json.org/json-zh.html</a></li>
<li><a href="http://www.nowamagic.net/javascript/js_JsonVSXML.php">JSON 与 XML 的比较</a></li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://witmax.cn/json-vs-xml.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Discuz和jQuery冲突的解决办法</title>
		<link>http://witmax.cn/discuz-jquery-conflict.html</link>
		<comments>http://witmax.cn/discuz-jquery-conflict.html#comments</comments>
		<pubDate>Wed, 03 Nov 2010 15:33:36 +0000</pubDate>
		<dc:creator>晴枫</dc:creator>
				<category><![CDATA[网页设计]]></category>
		<category><![CDATA[Discuz]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://witmax.cn/?p=1139</guid>
		<description><![CDATA[Discuz和jQuery有两个冲突的地方。 冲突一、Array.prototype.push函数冲突 Discuz 7.0版本为了兼容低版本的IE，重写了Array的push方法，代码位于include/common.js中，如下： 但以上代码没有做任何检测直接进行了覆盖导致冲突，解决办法是修改为如下代码： 此问题在Discuz 7.2中估计已经修复，因为在common.js中没有找到以上代码；Discuz_X1.5中同样未找到。 冲突二、$(id)函数冲突 Discuz使用了S()作为对象选取函数，刚好与jQuery默认的$()函数重合造成冲突，代码依旧位于include/common.js中，如下： 解决方法1： 将jquery.js在common.js之前载入，不然jquery的$()函数会覆盖common.js的$()函数；然后用到jQuery的$()函数的地方都用jQuery()代替。 解决方法2： 将jquery.js在common.js之后载入，在调用jQuery的函数前使用一下代码： 以上是将$()函数映射回原来的$()函数；之后便可以使用jq()或jQuery()来代替jQuery原来的$()函数，这里jq和jQuery相同，而$()函数则是原来Discuz的$()函数。 解决方法2也可使用jQuery.noConflict()的其他用法代替，参见后面的使用说明。 xprins同学提到Discuz_X1.5，特意安装试用了一下，使用以上方法可以解决问题，提供一下Discuz X1.5和jQuery冲突的解决办法给大家做参考。 ============= jQuery.noConflict() 使用说明 运行这个函数将变量$的控制权让渡给第一个实现它的那个库。这有助于确保jQuery不会与其他库的$对象发生冲突。 在运行这个函数后，就只能使用jQuery变量访问jQuery对象。例如，在要用到$(“div p”)的地方，就必须换成jQuery(“div p”)。 注意:这个函数必须在你导入jQuery文件之后，并且在导入另一个导致冲突的库之前使用。当然也应当在其他冲突的库被使用之前，除非jQuery是最后一个导入的。 恢复使用别名$，然后创建并执行一个函数，在这个函数的作用域中仍然将$作为jQuery的别名来使用。在这个函数中，原来的$对象是无效的。这个函数对于大多数不依赖于其他库的插件都十分有效。 可以通过jQuery.noConflict()级联函数来简化写法。 创建一个新的别名用以在接下来的库中使用jQuery对象。 把jQuery完全移动到另一个命名空间的对象上，如下 参考：http://api.jquery.com/jQuery.noConflict/]]></description>
			<content:encoded><![CDATA[<p>Discuz和jQuery有两个冲突的地方。</p>
<p><strong>冲突一、Array.prototype.push函数冲突</strong></p>
<p>Discuz 7.0版本为了兼容低版本的IE，重写了Array的push方法，代码位于include/common.js中，如下：</p>
<pre class="brush: jscript; title: ; notranslate">Array.prototype.push = function(value) {
   this[this.length] = value;
   return this.length;
}</pre>
<p>但以上代码没有做任何检测直接进行了覆盖导致冲突，解决办法是修改为如下代码：</p>
<p><span id="more-1139"></span></p>
<pre class="brush: jscript; title: ; notranslate">if(typeof Array.prototype.push === 'undefined') {
   Array.prototype.push = function(value) {
      this[this.length] = value;
      return this.length;
   }
}</pre>
<p>此问题在Discuz 7.2中估计已经修复，因为在common.js中没有找到以上代码；Discuz_X1.5中同样未找到。</p>
<p><strong>冲突二、$(id)函数冲突</strong></p>
<p>Discuz使用了S()作为对象选取函数，刚好与jQuery默认的$()函数重合造成冲突，代码依旧位于include/common.js中，如下：</p>
<pre class="brush: jscript; title: ; notranslate">function $(id) {
   return document.getElementById(id);
}</pre>
<p>解决方法1：</p>
<p>将jquery.js在common.js之前载入，不然jquery的$()函数会覆盖common.js的$()函数；然后用到jQuery的$()函数的地方都用jQuery()代替。</p>
<p>解决方法2：</p>
<p>将jquery.js在common.js之后载入，在调用jQuery的函数前使用一下代码：</p>
<pre class="brush: jscript; title: ; notranslate">var jq = jQuery.noConflict();
</pre>
<p>以上是将$()函数映射回原来的$()函数；之后便可以使用jq()或jQuery()来代替jQuery原来的$()函数，这里jq和jQuery相同，而$()函数则是原来Discuz的$()函数。</p>
<p>解决方法2也可使用jQuery.noConflict()的其他用法代替，参见后面的使用说明。</p>
<p>xprins同学提到Discuz_X1.5，特意安装试用了一下，使用以上方法可以解决问题，提供一下<a href="http://witmax.cn/discuz-x-jquery-conflict.html">Discuz X1.5和jQuery冲突的解决办法</a>给大家做参考。</p>
<p>=============</p>
<p><strong>jQuery.noConflict() 使用说明</strong></p>
<p>运行这个函数将变量$的控制权让渡给第一个实现它的那个库。这有助于确保jQuery不会与其他库的$对象发生冲突。</p>
<p>在运行这个函数后，就只能使用jQuery变量访问jQuery对象。例如，在要用到$(“div p”)的地方，就必须换成jQuery(“div p”)。</p>
<p>注意:这个函数必须在你导入jQuery文件之后，并且在导入另一个导致冲突的库之前使用。当然也应当在其他冲突的库被使用之前，除非jQuery是最后一个导入的。</p>
<pre class="brush: jscript; title: ; notranslate">jQuery.noConflict();
// 使用 jQuery
jQuery(&quot;div p&quot;).hide();
// 使用其他库的 $()
$(&quot;content&quot;).style.display = 'none'; </pre>
<p>恢复使用别名$，然后创建并执行一个函数，在这个函数的作用域中仍然将$作为jQuery的别名来使用。在这个函数中，原来的$对象是无效的。这个函数对于大多数不依赖于其他库的插件都十分有效。</p>
<pre class="brush: jscript; title: ; notranslate">jQuery.noConflict();
(function($) {
 $(function() {
  // 使用 $ 作为 jQuery 别名的代码
 });
})(jQuery);
// 其他用 $ 作为别名的库的代码</pre>
<p><code>可以通过jQuery.noConflict()级联函数来简化写法。</code></p>
<pre class="brush: jscript; title: ; notranslate">jQuery.noConflict()(function(){
  // 使用 $ 作为 jQuery 别名的代码
});
// 其他用 $ 作为别名的库的代码</pre>
<p>创建一个新的别名用以在接下来的库中使用jQuery对象。</p>
<pre class="brush: jscript; title: ; notranslate">var j = jQuery.noConflict();
// 使用 jQuery 的代码
j(&quot;div p&quot;).hide();
// 使用其他库的 $() 的代码
$(&quot;content&quot;).style.display = 'none';</pre>
<p>把jQuery完全移动到另一个命名空间的对象上，如下</p>
<pre class="brush: jscript; title: ; notranslate">var dom = {};
dom.query =jQuery.noConflict(true);
// 使用新 jQuery 的代码
dom.query(&quot;div p&quot;).hide();
// 使用其他库的 $() 的代码
$(&quot;content&quot;).style.display = 'none';
// 使用 jQuery 的代码
jQuery(&quot;div &amp;gt; p&quot;).hide();</pre>
<p>参考：<a href="http://api.jquery.com/jQuery.noConflict/">http://api.jquery.com/jQuery.noConflict/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://witmax.cn/discuz-jquery-conflict.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>【jQuery】增加页面组合快捷键</title>
		<link>http://witmax.cn/jquery-hot-key.html</link>
		<comments>http://witmax.cn/jquery-hot-key.html#comments</comments>
		<pubDate>Fri, 08 Oct 2010 08:52:27 +0000</pubDate>
		<dc:creator>晴枫</dc:creator>
				<category><![CDATA[网页设计]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://witmax.cn/?p=1037</guid>
		<description><![CDATA[jQuery是个优雅的框架，可以方便地用js实现页面快捷键的功能，代码很简单 另外，也可以借助jquery.hotkeys插件来完成，快捷键多且需要灵活增删时会比较方便，只是用下来有个问题，配合jQuery 3.4.2的时候按键事件会被重复触发，没去研究如何解决，有兴趣的同学可以和插件作者交流下。 附：键盘键码表 字母和数字键的键码值(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 [...]]]></description>
			<content:encoded><![CDATA[<p>jQuery是个优雅的框架，可以方便地用js实现页面快捷键的功能，代码很简单</p>
<pre class="brush: jscript; title: ; notranslate">$(document).keypress(function(e){
        if(e.ctrlKey &amp;&amp; e.which == 13) { 
                 alert(&quot;You clicked Ctrl+Enter&quot;);
        } else if (e.shiftKey &amp;&amp; e.which==13) {
                alert(&quot;You clicked Shift+Enter&quot;); 
       }  else if (e.altKey &amp;&amp; e.which==65) {
                alert(&quot;You clicked Alt+A&quot;); 
       }      
})</pre>
<p><span id="more-1037"></span>另外，也可以借助<a href="http://github.com/jeresig/jquery.hotkeys/" target="_blank">jquery.hotkeys插件</a>来完成，快捷键多且需要灵活增删时会比较方便，只是用下来有个问题，配合jQuery 3.4.2的时候按键事件会被重复触发，没去研究如何解决，有兴趣的同学可以和插件作者交流下。</p>
<p>附：键盘键码表</p>
<table border="1" cellspacing="0" cellpadding="5" rules="all" bordercolor="#333333">
<tbody>
<tr>
<td colspan="8">字母和数字键的键码值(keyCode)</td>
</tr>
<tr>
<td>按键</td>
<td>键码</td>
<td>按键</td>
<td>键码</td>
<td>按键</td>
<td>键码</td>
<td>按键</td>
<td>键码</td>
</tr>
<tr>
<td>A</td>
<td>65</td>
<td>J</td>
<td>74</td>
<td>S</td>
<td>83</td>
<td>1</td>
<td>49</td>
</tr>
<tr>
<td>B</td>
<td>66</td>
<td>K</td>
<td>75</td>
<td>T</td>
<td>84</td>
<td>2</td>
<td>50</td>
</tr>
<tr>
<td>C</td>
<td>67</td>
<td>L</td>
<td>76</td>
<td>U</td>
<td>85</td>
<td>3</td>
<td>51</td>
</tr>
<tr>
<td>D</td>
<td>68</td>
<td>M</td>
<td>77</td>
<td>V</td>
<td>86</td>
<td>4</td>
<td>52</td>
</tr>
<tr>
<td>E</td>
<td>69</td>
<td>N</td>
<td>78</td>
<td>W</td>
<td>87</td>
<td>5</td>
<td>53</td>
</tr>
<tr>
<td>F</td>
<td>70</td>
<td>O</td>
<td>79</td>
<td>X</td>
<td>88</td>
<td>6</td>
<td>54</td>
</tr>
<tr>
<td>G</td>
<td>71</td>
<td>P</td>
<td>80</td>
<td>Y</td>
<td>89</td>
<td>7</td>
<td>55</td>
</tr>
<tr>
<td>H</td>
<td>72</td>
<td>Q</td>
<td>81</td>
<td>Z</td>
<td>90</td>
<td>8</td>
<td>56</td>
</tr>
<tr>
<td>I</td>
<td>73</td>
<td>R</td>
<td>82</td>
<td>0</td>
<td>48</td>
<td>9</td>
<td>57</td>
</tr>
</tbody>
</table>
<p> </p>
<table border="1" cellspacing="0" cellpadding="5" rules="all" bordercolor="#333333">
<tbody>
<tr>
<td colspan="4">数字键盘上的键的键码值(keyCode)</td>
<td colspan="4">功能键键码值(keyCode)</td>
</tr>
<tr>
<td>按键</td>
<td>键码</td>
<td>按键</td>
<td>键码</td>
<td>按键</td>
<td>键码</td>
<td>按键</td>
<td>键码</td>
</tr>
<tr>
<td>0</td>
<td>96</td>
<td>8</td>
<td>104</td>
<td>F1</td>
<td>112</td>
<td>F7</td>
<td>118</td>
</tr>
<tr>
<td>1</td>
<td>97</td>
<td>9</td>
<td>105</td>
<td>F2</td>
<td>113</td>
<td>F8</td>
<td>119</td>
</tr>
<tr>
<td>2</td>
<td>98</td>
<td>*</td>
<td>106</td>
<td>F3</td>
<td>114</td>
<td>F9</td>
<td>120</td>
</tr>
<tr>
<td>3</td>
<td>99</td>
<td>+</td>
<td>107</td>
<td>F4</td>
<td>115</td>
<td>F10</td>
<td>121</td>
</tr>
<tr>
<td>4</td>
<td>100</td>
<td>Enter</td>
<td>108</td>
<td>F5</td>
<td>116</td>
<td>F11</td>
<td>122</td>
</tr>
<tr>
<td>5</td>
<td>101</td>
<td>-</td>
<td>109</td>
<td>F6</td>
<td>117</td>
<td>F12</td>
<td>123</td>
</tr>
<tr>
<td>6</td>
<td>102</td>
<td>.</td>
<td>110</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>7</td>
<td>103</td>
<td>/</td>
<td>111</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
</tbody>
</table>
<p> </p>
<table border="1" cellspacing="0" cellpadding="5" rules="all" bordercolor="#333333">
<tbody>
<tr>
<td colspan="8">控制键键码值(keyCode)</td>
</tr>
<tr>
<td>按键</td>
<td>键码</td>
<td>按键</td>
<td>键码</td>
<td>按键</td>
<td>键码</td>
<td>按键</td>
<td>键码</td>
</tr>
<tr>
<td>BackSpace</td>
<td>8</td>
<td>Esc</td>
<td>27</td>
<td>Right Arrow</td>
<td>39</td>
<td>-_</td>
<td>189</td>
</tr>
<tr>
<td>Tab</td>
<td>9</td>
<td>Spacebar</td>
<td>32</td>
<td>Dw Arrow</td>
<td>40</td>
<td>.&gt;</td>
<td>190</td>
</tr>
<tr>
<td>Clear</td>
<td>12</td>
<td>Page Up</td>
<td>33</td>
<td>Insert</td>
<td>45</td>
<td>/?</td>
<td>191</td>
</tr>
<tr>
<td>Enter</td>
<td>13</td>
<td>Page Down</td>
<td>34</td>
<td>Delete</td>
<td>46</td>
<td>`~</td>
<td>192</td>
</tr>
<tr>
<td>Shift</td>
<td>16</td>
<td>End</td>
<td>35</td>
<td>Num Lock</td>
<td>144</td>
<td>[{</td>
<td>219</td>
</tr>
<tr>
<td>Control</td>
<td>17</td>
<td>Home</td>
<td>36</td>
<td>;:</td>
<td>186</td>
<td>"|</td>
<td>220</td>
</tr>
<tr>
<td>Alt</td>
<td>18</td>
<td>Left Arrow</td>
<td>37</td>
<td>=+</td>
<td>187</td>
<td>]}</td>
<td>221</td>
</tr>
<tr>
<td>Cape Lock</td>
<td>20</td>
<td>Up Arrow</td>
<td>38</td>
<td>,&lt;</td>
<td>188</td>
<td>&#8216;”</td>
<td>222</td>
</tr>
</tbody>
</table>
]]></content:encoded>
			<wfw:commentRss>http://witmax.cn/jquery-hot-key.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

