<?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://pwwang.com/category/technology/js-javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://pwwang.com</link>
	<description>专注技术,用心生活!</description>
	<lastBuildDate>Fri, 11 Nov 2011 11:29:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>js控制表单提交经典一例</title>
		<link>http://pwwang.com/2011/04/06/js_html_cannon_problem/</link>
		<comments>http://pwwang.com/2011/04/06/js_html_cannon_problem/#comments</comments>
		<pubDate>Wed, 06 Apr 2011 14:11:43 +0000</pubDate>
		<dc:creator>pwwang</dc:creator>
				<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://pwwang.com/?p=994</guid>
		<description><![CDATA[扯句闲话，不知道为什么，偶没写什么反动言论啊，IP被墙，换IP后国内才能访问。。。搞得上篇都整E文，心想反正国人看不到了。。。现在终于可以用上母语了。。。

言归正传。

前两天和同事调程序，遇上js控制表单提交经典案例。

<span class="readmore"><a href="http://pwwang.com/2011/04/06/js_html_cannon_problem/" title="js控制表单提交经典一例">Read More: 654 Words Totally</a></span>]]></description>
			<content:encoded><![CDATA[<p>扯句闲话，不知道为什么，偶没写什么反动言论啊，IP被墙，换IP后国内才能访问。。。搞得上篇都整E文，心想反正国人看不到了。。。现在终于可以用上母语了。。。</p>
<p>言归正传。</p>
<p>前两天和同事调程序，遇上js控制表单提交经典案例。</p>
<p>事情是这样的，他用js控制表单提交，结果发现无论如何也无论提交数据。但是点submit按钮却可以。</p>
<p>他的表单大致如此：</p>
<p><span id="more-994"></span></p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;form</span> <span style="color: #000066;">action</span>=<span style="color: #ff0000;">&quot;www.google.com&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;input</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;property&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;input</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">type</span>=submit <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;input</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;button&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;sub&quot;</span> <span style="color: #000066;">onclick</span>=<span style="color: #ff0000;">&quot;javascript:this.form.submit()&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/form<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>&nbsp;</p>
<p>表现就是 点submit可以提交，点sub无法提交。。。</p>
<p>于是怀疑form的submit函数有问题，尝试打印出form的所有属性，发现submit的值是字符串submit，</p>
<p>而不是一个function，看到submit的name后，恍然大悟，</p>
<p>button的name值设为submit后，它成为了form的一个属性，从而覆盖了form原有的submit函数。。</p>
<p>修正方法也很简单，将submit的name改为其他值就可以了。</p>
<p>变量名有意识的避开潜在关键字，还是一个挺好的习惯。</p>
<p>正是因为我之前在写表单的时候都避开了类似的关键字（从未出过类似问题），</p>
<p>所以看到他的表单的submit的时候只是觉得有点不妥，却没有警觉起来。</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://pwwang.com/2011/04/06/js_html_cannon_problem/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>A more elegant way to write OO-JavaScript</title>
		<link>http://pwwang.com/2011/01/13/a-more-elegant-way-to-write-oo-javascript/</link>
		<comments>http://pwwang.com/2011/01/13/a-more-elegant-way-to-write-oo-javascript/#comments</comments>
		<pubDate>Thu, 13 Jan 2011 14:01:35 +0000</pubDate>
		<dc:creator>pwwang</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://pwwang.com/?p=978</guid>
		<description><![CDATA[&#160;

JavaScript IS an OO language itself.

I just wrote a jQuery plugin to realize a C-like class writing style.

<span class="readmore"><a href="http://pwwang.com/2011/01/13/a-more-elegant-way-to-write-oo-javascript/" title="A more elegant way to write OO-JavaScript">Read More: 2731 Words Totally</a></span>]]></description>
			<content:encoded><![CDATA[<p>&nbsp;</p>
<p>JavaScript IS an OO language itself.</p>
<p>I just wrote a jQuery plugin to realize a C-like class writing style.</p>
<p>I&#8217;ve used just one of the jQuery functions, $.extend. So if you&#8217;d like to make it standalone, just realize $.extend yourself then you make it.</p>
<p>Now see the code, I&#8217;ve made comments in it.</p>
<p><span id="more-978"></span></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/**
 * @Author: pwwang(pwwang@pwwang.com)
 * @Function: make a more elegant way to write OO-JavaScript
 * @Note: presevered member name: 
 *   _init -&gt; constructor, 
 *   _super -&gt; super class which this class inherited.
 **/</span>
&nbsp;
<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
    $.<span style="color: #003366; font-weight: bold;">Class</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>opts<span style="color: #339933;">,</span> ext<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">//deepcopy, opts won't change</span>
        <span style="color: #003366; font-weight: bold;">var</span> temp_opts <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
        $.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span> temp_opts<span style="color: #339933;">,</span> opts <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
        <span style="color: #006600; font-style: italic;">// temporarily store the super class</span>
        <span style="color: #003366; font-weight: bold;">var</span> _super <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span> 
&nbsp;
        <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> ext <span style="color: #339933;">&amp;&amp;</span> $.<span style="color: #660066;">isFunction</span><span style="color: #009900;">&#40;</span>ext<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #006600; font-style: italic;">// deepcopy ext.prototype that it won't change</span>
            <span style="color: #003366; font-weight: bold;">var</span> ext_prttype <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
            $.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span> ext_prttype<span style="color: #339933;">,</span> ext.<span style="color: #660066;">prototype</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #006600; font-style: italic;">// class extension</span>
            $.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span> ext_prttype<span style="color: #339933;">,</span> temp_opts<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #006600; font-style: italic;">// give it back to temp_opts</span>
            $.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span> temp_opts<span style="color: #339933;">,</span> ext_prttype <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #006600; font-style: italic;">// get ext.prototype store in _super</span>
            $.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span> _super<span style="color: #339933;">,</span> ext.<span style="color: #660066;">prototype</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #006600; font-style: italic;">// initialize this.super</span>
            temp_opts._super <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">var</span> <span style="color: #003366; font-weight: bold;">CLASS</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">var</span> t <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span>
            <span style="color: #006600; font-style: italic;">// class initialization</span>
            <span style="color: #003366; font-weight: bold;">CLASS</span>.<span style="color: #660066;">prototype</span>._init.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span>t<span style="color: #339933;">,</span> arguments<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #006600; font-style: italic;">// realize this.super, try to apply THIS to the functions.</span>
            <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">var</span> k <span style="color: #000066; font-weight: bold;">in</span> _super <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                temp_opts._super<span style="color: #009900;">&#91;</span>k<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> $.<span style="color: #660066;">isFunction</span><span style="color: #009900;">&#40;</span>_super<span style="color: #009900;">&#91;</span>k<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> 
                <span style="color: #339933;">?</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>fn<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                        fn.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span>t<span style="color: #339933;">,</span> arguments<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>_super<span style="color: #009900;">&#91;</span>k<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> _super<span style="color: #009900;">&#91;</span>k<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// set other variables or functions</span>
        $.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">CLASS</span>.<span style="color: #660066;">prototype</span><span style="color: #339933;">,</span> temp_opts<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// use static impl to set</span>
        <span style="color: #003366; font-weight: bold;">CLASS</span>.<span style="color: #660066;">impl</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>im<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            $.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">CLASS</span>.<span style="color: #660066;">prototype</span><span style="color: #339933;">,</span> im<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">CLASS</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>&nbsp;</p>
<p>Here are some samples to show how to use it.</p>
<p>&nbsp;</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;html&gt;
&lt;head&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;jquery.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;jquery.class.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
$(function(){
    var log = function(v){
        (window.console &amp;&amp; window.console.log) ? window.console.log(v) : window.alert(v);
    }
&nbsp;
    var Anim = $.Class({
        _init: function(weight){
            this.weight = weight;
        },
        eat: function(){
            this.weight ++;
        },
        excrete: function(){
            this.weight --;
        },
        getWeight: function(){
            return this.weight;
        }
    });
&nbsp;
    var Cat = $.Class({
        _init: function(name, weight){ // override
            this.name = name;
            this.weight = weight;
        }
    }, Anim); // Inheritance 
&nbsp;
    Cat.impl({
        exercise : function(){
            this.weight -= 2;
        },
        eat: function(){
            if( arguments.length ){ // polymorphism
                this.weight += arguments[0];
            } else {
                this._super.eat();
            }
        }
    });
&nbsp;
    var Dog = $.Class({
        _init: function(name, weight){
            this.name = name;
            this.weight = weight;
        },
        eat: function(){
            this.weight += 2;
        }
    }, Anim);
&nbsp;
    var myAnim = new Anim(50);
    log(myAnim.getWeight()); // 50
    myAnim.eat();
    log(myAnim.getWeight()); // 51
    myAnim.excrete();
    log(myAnim.getWeight()); // 50
&nbsp;
    var myCat = new Cat('cat', 50);
    log(myCat.getWeight()); // 50
    myCat.eat();
    log(myCat.getWeight()); // 51
    myCat.eat(2);
    log(myCat.getWeight()); // 53
    myCat.excrete();
    log(myCat.getWeight()); // 52
    myCat.exercise();
    log(myCat.getWeight()); // 50
&nbsp;
    var myDog = new Dog('dog', 50);
    log(myDog.getWeight()); // 50
    myDog.eat();
    log(myDog.getWeight()); // 52
    myDog.excrete();
    log(myDog.getWeight()); // 51
&nbsp;
&nbsp;
});
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></div></div>

<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://pwwang.com/2011/01/13/a-more-elegant-way-to-write-oo-javascript/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>简单写了个jquery的鼠标提示文字插件</title>
		<link>http://pwwang.com/2010/08/27/jquery_tip/</link>
		<comments>http://pwwang.com/2010/08/27/jquery_tip/#comments</comments>
		<pubDate>Fri, 27 Aug 2010 01:51:07 +0000</pubDate>
		<dc:creator>pwwang</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://pwwang.com/?p=968</guid>
		<description><![CDATA[&#160;

效果图:



<span class="readmore"><a href="http://pwwang.com/2010/08/27/jquery_tip/" title="简单写了个jquery的鼠标提示文字插件">Read More: 1137 Words Totally</a></span>]]></description>
			<content:encoded><![CDATA[<p>&nbsp;</p>
<p>效果图:</p>
<p><a href="http://pwwang.com/wp-content/uploads/2010/08/jquery.tip_.js.jpg"><img width="347" height="198" title="jquery.tip.js" src="http://pwwang.com/wp-content/uploads/2010/08/jquery.tip_.js.jpg" style="clear: both;" alt="" /></a></p>
<p><span id="more-968"></span>调用:</p>
<p>
<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;html<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;head<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;script</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;js/jquery.js&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;script</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;js/jquery.tip.js&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
$(function(){
    $('div').tip('This is a multipul style tip. I\'m <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;font</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;font-weight:bold&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>bold<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/font<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>.<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;br</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>Line break. I\'m <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;font</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;color:red&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>red<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/font<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>. Images are also availale. <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;br</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span> <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;img</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;http://pwwang.com/logo.gif&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;</span></span>');
});
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/head<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;float:left; width:300px; height:300px; background:#f90;&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>handler<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/html<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

</p>
<p>源码:</p>
<p>
<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    $.<span style="color: #660066;">tip</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>src<span style="color: #339933;">,</span> content<span style="color: #339933;">,</span> cls<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">var</span> $tdiv <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div /&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        $tdiv.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #3366CC;">'position'</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'absolute'</span><span style="color: #339933;">,</span>
            <span style="color: #3366CC;">'display'</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'none'</span><span style="color: #339933;">,</span>
            <span style="color: #3366CC;">'z-index'</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'9999'</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>content<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">appendTo</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'body'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> cls <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            $tdiv.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span>cls<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
            $tdiv.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
                <span style="color: #3366CC;">'border'</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">'1px solid #969696'</span><span style="color: #339933;">,</span>
                <span style="color: #3366CC;">'background'</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">'#ffe'</span><span style="color: #339933;">,</span>
                <span style="color: #3366CC;">'font-size'</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">'12px'</span><span style="color: #339933;">,</span>
                <span style="color: #3366CC;">'padding'</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">'2px'</span>
            <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">var</span> move <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            $tdiv.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
                <span style="color: #3366CC;">'top'</span> <span style="color: #339933;">:</span> <span style="color: #009900;">&#40;</span>e.<span style="color: #660066;">pageY</span><span style="color: #339933;">+</span><span style="color: #CC0000;">23</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'px'</span><span style="color: #339933;">,</span>
                <span style="color: #3366CC;">'left'</span> <span style="color: #339933;">:</span> <span style="color: #009900;">&#40;</span>e.<span style="color: #660066;">pageX</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'px'</span>
            <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
        $<span style="color: #009900;">&#40;</span>src<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">mouseenter</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            $tdiv.<span style="color: #660066;">show</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            $<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mousemove'</span><span style="color: #339933;">,</span> move<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        $<span style="color: #009900;">&#40;</span>src<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">mouseleave</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            $tdiv.<span style="color: #660066;">hide</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            $<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">unbind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mousemove'</span><span style="color: #339933;">,</span> move<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// content: what is the tip</span>
    <span style="color: #006600; font-style: italic;">// cls: the style class of the tip div</span>
    $.<span style="color: #660066;">fn</span>.<span style="color: #660066;">tip</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>content<span style="color: #339933;">,</span> cls<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">new</span> $.<span style="color: #660066;">tip</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> content<span style="color: #339933;">,</span> cls<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span>    
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>
</p>
]]></content:encoded>
			<wfw:commentRss>http://pwwang.com/2010/08/27/jquery_tip/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>谈HTML表单元素的一种写法</title>
		<link>http://pwwang.com/2010/08/26/talk_about_an_insertion_of_html_form/</link>
		<comments>http://pwwang.com/2010/08/26/talk_about_an_insertion_of_html_form/#comments</comments>
		<pubDate>Thu, 26 Aug 2010 07:38:43 +0000</pubDate>
		<dc:creator>pwwang</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://pwwang.com/?p=964</guid>
		<description><![CDATA[&#160;

默认的表单(form)是有margin的, 所以插入的时候, 可能会使布局变形.

有时候表单内的元素较多, 需要排版, 较为方便的还是table.

<span class="readmore"><a href="http://pwwang.com/2010/08/26/talk_about_an_insertion_of_html_form/" title="谈HTML表单元素的一种写法">Read More: 921 Words Totally</a></span>]]></description>
			<content:encoded><![CDATA[<p>&nbsp;</p>
<p>默认的表单(form)是有margin的, 所以插入的时候, 可能会使布局变形.</p>
<p>有时候表单内的元素较多, 需要排版, 较为方便的还是table.</p>
<p>为了消除表单给页面布局带来的影响, 我常常喜欢这样插入表单:</p>
<p>
<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;table<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;form</span> <span style="color: #000066;">method</span>=<span style="color: #ff0000;">&quot;post&quot;</span> <span style="color: #000066;">action</span>=<span style="color: #ff0000;">&quot;?&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;form1&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;form1&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;tr<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;td<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;input</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/td<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/tr<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/form<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/table<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

</p>
<p><span id="more-964"></span></p>
<p>这样就没有表单带来的布局影响了.</p>
<p>然而, 当我用js来操作时, IE下影响不大, 但在FF下就出现问题了:</p>
<p>&nbsp;</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#form1'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">//用纯js操作效果是一样的</span></pre></div></div>

<p>&nbsp;</p>
<p>IE下正常,可以得到表单的innerHTML, 但FF下却为空.</p>
<p>如果我用js在表单中插入一个元素, 但来alert,发现只有插入的那个元素的html:</p>
<p>&nbsp;</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#form1'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;input type=&quot;text&quot; name=&quot;textfield&quot; /&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#form1'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">//FF中结果是&lt;input type=&quot;text&quot; name=&quot;textfield&quot; &gt;</span>
<span style="color: #006600; font-style: italic;">//ie中可以得到原有元素加上插入的元素的html</span></pre></div></div>

<p>&nbsp;</p>
<p>查看源代码发现, 原来FF把form的结束tag解释在了开始tag之后:</p>
<p>&nbsp;</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;table<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;form</span> <span style="color: #000066;">action</span>=<span style="color: #ff0000;">&quot;?&quot;</span> <span style="color: #000066;">method</span>=<span style="color: #ff0000;">&quot;post&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;form1&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;form1&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/form<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;tbody<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;tr<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;td<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;input</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;textfield&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/td<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/tr<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/tbody<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/table<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>&nbsp;</p>
<p>故而form的innerHTML为空.</p>
<p>正确的写法应该是这样的:</p>
<p>&nbsp;</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;style<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> /*可在css文件中定义*/
form{margin:0; padding0;}
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/style<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;form</span> <span style="color: #000066;">method</span>=<span style="color: #ff0000;">&quot;post&quot;</span> <span style="color: #000066;">action</span>=<span style="color: #ff0000;">&quot;?&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;form1&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;form1&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;table<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;tr<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;td<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;input</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/td<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/tr<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/table<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/form<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://pwwang.com/2010/08/26/talk_about_an_insertion_of_html_form/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Mine-Sweeping(扫雷) in jQuery/JavaScript</title>
		<link>http://pwwang.com/2009/10/31/mine-sweeping-in-jqueryjavascript/</link>
		<comments>http://pwwang.com/2009/10/31/mine-sweeping-in-jqueryjavascript/#comments</comments>
		<pubDate>Fri, 30 Oct 2009 16:01:09 +0000</pubDate>
		<dc:creator>pwwang</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[扫雷]]></category>

		<guid isPermaLink="false">http://pwwang.com/?p=772</guid>
		<description><![CDATA[Demo:&#160;&#160; http://pwwang.com/tools/mine-sweeping.html

Google Code Project Home Page:&#160; http://code.google.com/p/mine-sweep-in-jquery-javascript/



<span class="readmore"><a href="http://pwwang.com/2009/10/31/mine-sweeping-in-jqueryjavascript/" title="Mine-Sweeping(扫雷) in jQuery/JavaScript">Read More: 616 Words Totally</a></span>]]></description>
			<content:encoded><![CDATA[<p><a target="_blank" href="http://pwwang.com/tools/mine-sweeping.html"><img title="Mine_Sweeping_in_jQuery_JavaScript" border="0" alt="Mine_Sweeping_in_jQuery_JavaScript" width="650" height="418" style="clear: both" src="http://pwwang.com/wp-content/uploads/2009/10/Mine_Sweeping_in_jQuery_JavaScript.png" /></a></p>
<p><strong><big>Demo:</big></strong>&nbsp;&nbsp; <a target="_blank" href="http://pwwang.com/tools/mine-sweeping.html">http://pwwang.com/tools/mine-sweeping.html</a></p>
<p><big><strong>Google Code Project Home Page:&nbsp; </strong></big><a target="_blank" href="http://code.google.com/p/mine-sweep-in-jquery-javascript/">http://code.google.com/p/mine-sweep-in-jquery-javascript/</a></p>
<p><span id="more-772"></span></p>
<h1>Introduction</h1>
<p>A simulation of windows mine-sweeping(扫雷) in jQuery/JavaScript<a href="/p/mine-sweep-in-jquery-javascript/w/edit/JavaScript">?</a></p>
<h1>Information</h1>
<p>pwwang <a rel="nofollow" href="http://pwwang.com">http://pwwang.com</a></p>
<p>page <a rel="nofollow" href="http://pwwang.com/technology/js-javascript/mine-sweeping-in-jquery-javascript/">http://pwwang.com/technology/js-javascript/mine-sweeping-in-jquery-javascript/</a></p>
<p>demo <a rel="nofollow" href="http://pwwang.com/tools/mine-sweeping.html">http://pwwang.com/tools/mine-sweeping.html</a></p>
<h1>Details</h1>
<ul>
<li>Performs perfectly in Firefox</li>
<li>Works well in IE but not ideally</li>
<li>Basicly realize all the functions of all windows mine-sweeping has</li>
<li>Especially realize an event that the left-button and right-button clicks at the &quot;same&quot; time &#8212;- that is, it differs in a very short time,and then the event will fire.</li>
</ul>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://pwwang.com/2009/10/31/mine-sweeping-in-jqueryjavascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JavaScript中左右键“同时”点击(click,mousedown,mouseup)事件</title>
		<link>http://pwwang.com/2009/10/29/left-right-simultaneously-click-in-javascript/</link>
		<comments>http://pwwang.com/2009/10/29/left-right-simultaneously-click-in-javascript/#comments</comments>
		<pubDate>Thu, 29 Oct 2009 01:35:09 +0000</pubDate>
		<dc:creator>pwwang</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[左右键同时点击]]></category>

		<guid isPermaLink="false">http://pwwang.com/?p=760</guid>
		<description><![CDATA[&#160;

之所以同时加上引号，是因为真正的同时，除了机器人，是不可能做到的。

这个是利用jQuery的事件管理机制来实现的，并把它写成了一个jQuery插件。

<span class="readmore"><a href="http://pwwang.com/2009/10/29/left-right-simultaneously-click-in-javascript/" title="JavaScript中左右键“同时”点击(click,mousedown,mouseup)事件">Read More: 3939 Words Totally</a></span>]]></description>
			<content:encoded><![CDATA[<p>&nbsp;</p>
<p>之所以同时加上引号，是因为真正的同时，除了机器人，是不可能做到的。</p>
<p>这个是利用jQuery的事件管理机制来实现的，并把它写成了一个jQuery插件。</p>
<p>在Firefox中表现完美， 在IE中能够实现想要的功能，但是总是有些不尽如人意。</p>
<p>在IE中左键的button值是1,右键是0；而在Firefox中左键是0,右键是2.</p>
<p>问题倒不在这里，问题在于，如果你在IE中先点右键，再很短的时间内再点左键，你会发现得到的button值是3！并且如果是这么简单也就好了，直接检测button值，如果是3,就直接执行事件就行了。可是问题总不是想象中的那样简单，在button＝3的事件执行完之后，IE还会激发一个button=1的事件（先点的右键，那点的左键）。这在处理起来就麻烦多了。</p>
<p>我的思路和网上想实现这类功能的人的思路大致相同，也是通过一个计时器来判断。</p>
<p>具体思路是这样的：</p>
<p><span id="more-760"></span>这个插件可以处理鼠标左右键的同时点击（click），鼠标按下（mousedown），鼠标弹起（mouseup）事件。在讲思路的时候就以点击为</p>
<p>例。</p>
<p>首先我们必须先找出所有绑定为点击的事件，unbind它们，因为要阻止它们在一点击鼠标的时候就执行。</p>
<p>在找这些事件的时候还是颇费了一番功夫的。因为jQuery的源码还没有真正的读通。所以机制也并没有弄很清楚明白。jQuery将事件存在了一个缓存里面（jQuery.cache），但是如果从这个缓存里把这些事件取出来？关键问题是找到元素对应的在缓存的位置，我找遍了jQuery(&#8230;)就是没有这样的公有方法。于是用了一个小trick，我们知道jQuery允许我们给一个元素绑定data，而这个data也是存在缓存里的。所以我就可以赋一个唯一的属性值给data（时间戳或者随机数），再后再通过这个唯一值找到这个元素并找到存储的事件。</p>
<p>这时候我们就可以处理左右键的事件了。因为无论左键还是右键先点，我们要达到的效果是一样的，所以这里只说一下其中一个键点击的事件。</p>
<p>当点击一个键的时候，我们先需要先判断另一个键是否已经点击，如果还没有点击，这时候判断一下时间差（这个是专为IE设计的。。。因为IE会引起一个button=1的事件，在button＝3的事件执行之后，就把时间变量设置为当前时间），如果不在时间差内，就等待另一个按键的点击。如果另一个键已经点击了的话，那就直接判断时间差，在时间差内就执行给定的事件；如果不在，就执行原有的事件。</p>
<p><span style="color: rgb(153, 51, 0);">这样，在bindLR后，也不影响原来已经bind的事件的执行（会有设定时间的延时，为了等待另一个键的点击），在unbindLR之后，也能够恢复原来已经bind的事件，我没有采用循环绑定，所以在绑定了bindLR事件之后，无法再用unbind来原有事件的绑定，并且，unbindLR现在的功能有限，只能解除所有的bindLR事件，并不能解除指定事件的绑定。</span></p>
<p>jquery.LRmouse.js</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/**
 *
 * jquery.LRmouse 1.0.0
 * 
 * Author: pwwang ( http://pwwang.com )
 * 2009.10.26
 *
 * Bind an event handle to a listener that 
 * LButton and RButton of the mouse act
 * ( click, mousedown, mouseup ) &quot;simultaneously&quot;
 *
 * It works on both IE and Firefox, distinguishedly,
 * however, it performs perfectly in Firefox; not
 * exactly ideally in IE.
 *
 **/</span>
<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
    $.<span style="color: #660066;">bindLR</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> elem<span style="color: #339933;">,</span> type<span style="color: #339933;">,</span> data<span style="color: #339933;">,</span> fn<span style="color: #339933;">,</span> intv <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">var</span> L_BUTTON <span style="color: #339933;">=</span> $.<span style="color: #660066;">browser</span>.<span style="color: #660066;">msie</span> <span style="color: #339933;">?</span> <span style="color: #CC0000;">1</span> <span style="color: #339933;">:</span> <span style="color: #CC0000;">0</span> <span style="color: #339933;">;</span>
        <span style="color: #003366; font-weight: bold;">var</span> R_BUTTON <span style="color: #339933;">=</span> $.<span style="color: #660066;">browser</span>.<span style="color: #660066;">msie</span> <span style="color: #339933;">?</span> <span style="color: #CC0000;">0</span> <span style="color: #339933;">:</span> <span style="color: #CC0000;">2</span> <span style="color: #339933;">;</span>    
        <span style="color: #003366; font-weight: bold;">var</span> LR_BUTTON <span style="color: #339933;">=</span> <span style="color: #CC0000;">3</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// ie</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">var</span> orgType <span style="color: #339933;">=</span> type.<span style="color: #660066;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">var</span> L_flag <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #003366; font-weight: bold;">var</span> R_flag <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>                
&nbsp;
        <span style="color: #003366; font-weight: bold;">var</span> getTime <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #339933;">+</span><span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>        
&nbsp;
        <span style="color: #003366; font-weight: bold;">var</span> lrId <span style="color: #339933;">=</span> getTime<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #006600; font-style: italic;">// for finding the elem in $.cache</span>
        <span style="color: #006600; font-style: italic;">// a trick, 'cz I don't know how to</span>
        <span style="color: #006600; font-style: italic;">// find the function directly</span>
        elem.<span style="color: #660066;">data</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'lrId'</span><span style="color: #339933;">,</span> lrId<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
        <span style="color: #003366; font-weight: bold;">var</span> orgFns <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #000066; font-weight: bold;">in</span> $.<span style="color: #660066;">cache</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>  
        <span style="color: #006600; font-style: italic;">// find original binded functions</span>
            <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> $.<span style="color: #660066;">cache</span><span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'lrId'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> lrId <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                <span style="color: #003366; font-weight: bold;">var</span> fns <span style="color: #339933;">=</span> $.<span style="color: #660066;">cache</span><span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'events'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>orgType<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
                <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">var</span> j <span style="color: #000066; font-weight: bold;">in</span> fns <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                    orgFns.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span> fns<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    elem.<span style="color: #660066;">unbind</span><span style="color: #009900;">&#40;</span> orgType<span style="color: #339933;">,</span> fns<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
                <span style="color: #006600; font-style: italic;">//remove it</span>
                elem.<span style="color: #660066;">removeData</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'lrId'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
        elem.<span style="color: #660066;">data</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'lrTempEvents'</span><span style="color: #339933;">,</span>orgFns<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
        <span style="color: #003366; font-weight: bold;">var</span> timer <span style="color: #339933;">=</span> getTime<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003366; font-weight: bold;">var</span> timeOut<span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">var</span> reset <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            L_flag <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
            R_flag <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
            clearTimeout<span style="color: #009900;">&#40;</span> timeOut <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>        
&nbsp;
        <span style="color: #003366; font-weight: bold;">var</span> orgFnHandle <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #339933;">,</span> data<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            reset<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span>orgFns.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                orgFns<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span>elem<span style="color: #339933;">,</span> e<span style="color: #339933;">,</span> data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">var</span> L_event <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #339933;">,</span> data<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span>R_flag <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #006600; font-style: italic;">//waiting</span>
                <span style="color: #006600; font-style: italic;">// if LR_BUTTON, do NOT wait</span>
                <span style="color: #006600; font-style: italic;">// LR_BUTTON in ie will call L_BUTTON event</span>
                <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> getTime<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> timer <span style="color: #339933;">&gt;</span> intv <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>  
                    L_flag <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
                    timer <span style="color: #339933;">=</span> getTime<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    timeOut <span style="color: #339933;">=</span> setTimeout<span style="color: #009900;">&#40;</span> 
                        <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>orgFnHandle<span style="color: #009900;">&#40;</span>e<span style="color: #339933;">,</span> data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> 
                        intv<span style="color: #339933;">+</span><span style="color: #CC0000;">1</span>
                    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
                reset<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #003366; font-weight: bold;">var</span> deltT <span style="color: #339933;">=</span> getTime<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> timer<span style="color: #339933;">;</span>
                <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> deltT <span style="color: #339933;">&lt;=</span> intv <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                    fn.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span>elem<span style="color: #339933;">,</span> e<span style="color: #339933;">,</span> data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
                    orgFnHandle<span style="color: #009900;">&#40;</span>e<span style="color: #339933;">,</span> data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">var</span> R_event <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #339933;">,</span> data<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span>L_flag <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> getTime<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> timer <span style="color: #339933;">&gt;</span> intv <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                    R_flag <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
                    timer <span style="color: #339933;">=</span> getTime<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    timeOut <span style="color: #339933;">=</span> setTimeout<span style="color: #009900;">&#40;</span> 
                        <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>orgFnHandle<span style="color: #009900;">&#40;</span>e<span style="color: #339933;">,</span> data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> 
                        intv<span style="color: #339933;">+</span><span style="color: #CC0000;">1</span>
                    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span><span style="color: #009900;">&#123;</span>
                reset<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #003366; font-weight: bold;">var</span> deltT <span style="color: #339933;">=</span> getTime<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span>timer<span style="color: #339933;">;</span>
                <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> deltT <span style="color: #339933;">&lt;=</span> intv <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                    fn.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span>elem<span style="color: #339933;">,</span> e<span style="color: #339933;">,</span> data<span style="color: #009900;">&#41;</span>
                <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
                    orgFnHandle<span style="color: #009900;">&#40;</span>e<span style="color: #339933;">,</span> data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #003366; font-weight: bold;">var</span> eventHandle <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #339933;">,</span> data<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> L_BUTTON <span style="color: #339933;">===</span> e.<span style="color: #660066;">button</span> <span style="color: #009900;">&#41;</span>
                L_event <span style="color: #009900;">&#40;</span>e<span style="color: #339933;">,</span> data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> R_BUTTON <span style="color: #339933;">===</span> e.<span style="color: #660066;">button</span> <span style="color: #009900;">&#41;</span>
                R_event <span style="color: #009900;">&#40;</span>e<span style="color: #339933;">,</span> data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> LR_BUTTON <span style="color: #339933;">===</span> e.<span style="color: #660066;">button</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #006600; font-style: italic;">//ie</span>
                timer <span style="color: #339933;">=</span> getTime<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                fn.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span>elem<span style="color: #339933;">,</span> e<span style="color: #339933;">,</span> data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>    
&nbsp;
        <span style="color: #006600; font-style: italic;">//ban context menu;</span>
        elem.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'contextmenu'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
        <span style="color: #006600; font-style: italic;">//bind new</span>
        elem.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span> orgType<span style="color: #339933;">,</span> data<span style="color: #339933;">,</span> eventHandle <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    $.<span style="color: #660066;">unbindLR</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>elem<span style="color: #339933;">,</span> type<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        elem.<span style="color: #660066;">unbind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'contextmenu'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        elem.<span style="color: #660066;">unbind</span><span style="color: #009900;">&#40;</span> type <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003366; font-weight: bold;">var</span> orgType <span style="color: #339933;">=</span> type.<span style="color: #660066;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        elem.<span style="color: #660066;">unbind</span><span style="color: #009900;">&#40;</span> orgType <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    
&nbsp;
        <span style="color: #003366; font-weight: bold;">var</span> lrId <span style="color: #339933;">=</span> Math.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        elem.<span style="color: #660066;">data</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'lrId'</span><span style="color: #339933;">,</span> lrId<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
&nbsp;
        <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #000066; font-weight: bold;">in</span> $.<span style="color: #660066;">cache</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>  
            <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> $.<span style="color: #660066;">cache</span><span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'lrId'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> lrId <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                <span style="color: #003366; font-weight: bold;">var</span> fns <span style="color: #339933;">=</span> $.<span style="color: #660066;">cache</span><span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'lrTempEvents'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">var</span> j <span style="color: #000066; font-weight: bold;">in</span> fns <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                    elem.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span> orgType<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> fns<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span>elem<span style="color: #339933;">,</span>arguments<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
                <span style="color: #006600; font-style: italic;">//remove it</span>
                elem.<span style="color: #660066;">removeData</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'lrId'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                elem.<span style="color: #660066;">removeData</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'lrTempEvents'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    $.<span style="color: #660066;">fn</span>.<span style="color: #660066;">bindLR</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>type<span style="color: #339933;">,</span> data<span style="color: #339933;">,</span> fn<span style="color: #339933;">,</span> intv<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> $.<span style="color: #660066;">isFunction</span><span style="color: #009900;">&#40;</span> data <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            intv <span style="color: #339933;">=</span> fn <span style="color: #339933;">||</span> <span style="color: #CC0000;">500</span><span style="color: #339933;">;</span>
            fn <span style="color: #339933;">=</span> data<span style="color: #339933;">;</span>
            data <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
            data <span style="color: #339933;">=</span> data <span style="color: #339933;">||</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
            fn <span style="color: #339933;">=</span> fn <span style="color: #339933;">||</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
            intv <span style="color: #339933;">=</span> intv <span style="color: #339933;">||</span> <span style="color: #CC0000;">500</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #006600; font-style: italic;">// lr bind for $(...).trigger()</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span>type<span style="color: #339933;">,</span>data<span style="color: #339933;">,</span>fn<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>   
&nbsp;
        <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> type.<span style="color: #660066;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span><span style="color: #3366CC;">'lr'</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            $.<span style="color: #660066;">bindLR</span><span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> type<span style="color: #339933;">,</span> data<span style="color: #339933;">,</span> fn<span style="color: #339933;">,</span> intv <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    $.<span style="color: #660066;">fn</span>.<span style="color: #660066;">unbindLR</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>type<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        $.<span style="color: #660066;">unbindLR</span><span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span>type <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://pwwang.com/2009/10/29/left-right-simultaneously-click-in-javascript/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>JavaScript中的print_r</title>
		<link>http://pwwang.com/2009/10/27/print_r-in-javascript/</link>
		<comments>http://pwwang.com/2009/10/27/print_r-in-javascript/#comments</comments>
		<pubDate>Mon, 26 Oct 2009 16:59:03 +0000</pubDate>
		<dc:creator>pwwang</dc:creator>
				<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://pwwang.com/?p=754</guid>
		<description><![CDATA[&#160;

做JavaScript开发, 特别是稍微复杂一点的程序, 难免要用到JavaScript面对对象的特性, 现在JavaScript的调试工具逐渐多和强大起来, 然而有时候要看一个object的值, 还是很不方便, 这时候就开始想念php的print_r功能了, 它可以将数组和对象的皮剥得干干净净, 让你看得一清二楚.

网上已经有很多版本了,但都不太好用,没有达到想要的效果,我自己写了一个, 稍微看得清楚一点.

<span class="readmore"><a href="http://pwwang.com/2009/10/27/print_r-in-javascript/" title="JavaScript中的print_r">Read More: 1151 Words Totally</a></span>]]></description>
			<content:encoded><![CDATA[<p>&nbsp;</p>
<p>做JavaScript开发, 特别是稍微复杂一点的程序, 难免要用到JavaScript面对对象的特性, 现在JavaScript的调试工具逐渐多和强大起来, 然而有时候要看一个object的值, 还是很不方便, 这时候就开始想念php的print_r功能了, 它可以将数组和对象的皮剥得干干净净, 让你看得一清二楚.</p>
<p>网上已经有很多版本了,但都不太好用,没有达到想要的效果,我自己写了一个, 稍微看得清楚一点.</p>
<p>先看一下效果图:</p>
<p><a href="http://pwwang.com/wp-content/uploads/2009/10/js_print_r.png"><img width="396" height="465" border="0" title="js_print_r" alt="js_print_r" src="http://pwwang.com/wp-content/uploads/2009/10/js_print_r.png" style="clear: both;" /></a></p>
<p>图中我只截取了一个元素,下面还有几个元素没有截取, 从这张图中可以看出来这个object的结构是:</p>
<p><span style="font-family: Courier New;">[ <br />
&nbsp;&nbsp; {&nbsp; events: { <br />
&nbsp;&nbsp;&nbsp; unload: [ function(){...} ],<br />
&nbsp; &nbsp; load: [ function(){...} ]<br />
&nbsp;&nbsp; } },<br />
&nbsp; { &#8230; }<br />
]</span></p>
<p><span id="more-754"></span>功能特色:</p>
<p>1. 同一父级下的子元素,不同的层级显示不同的字体颜色</p>
<p>2. 同一层级的开始和结束的地方用 | 连接, 极好辩认</p>
<p>3. 显示数据类型</p>
<p>4. 如果类型是函数的话, 格式保持不变( IE和firefox下表现不同, 但能保持格式 )</p>
<p>&nbsp;源代码:</p>
<p>&nbsp;</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> print_r<span style="color: #009900;">&#40;</span>o<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #000066;">print</span><span style="color: #009900;">&#40;</span>o<span style="color: #339933;">,</span>i<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        i <span style="color: #339933;">=</span> i <span style="color: #339933;">||</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>  <span style="color: #006600; font-style: italic;">// how many tabs</span>
        <span style="color: #003366; font-weight: bold;">var</span> color <span style="color: #339933;">=</span> <span style="color: #3366CC;">'#'</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'00000'</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span>0x1000000<span style="color: #339933;">&lt;&lt;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">16</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #CC0000;">6</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
        <span style="color: #006600; font-style: italic;">// randomly generate a color</span>
        <span style="color: #003366; font-weight: bold;">var</span> indent <span style="color: #339933;">=</span> <span style="color: #3366CC;">'|    '</span>
        <span style="color: #006600; font-style: italic;">// form of a tab, you can use '|\t' instead</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">var</span> space <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">var</span> k<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> k<span style="color: #339933;">&lt;</span>i<span style="color: #339933;">;</span> k<span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            space <span style="color: #339933;">+=</span> indent<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #003366; font-weight: bold;">var</span> ret <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">var</span> <span style="color: #000066;">name</span> <span style="color: #000066; font-weight: bold;">in</span> o <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">var</span> val <span style="color: #339933;">=</span> o<span style="color: #009900;">&#91;</span><span style="color: #000066;">name</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>   
            ret <span style="color: #339933;">+=</span> space <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;font style=&quot;color:'</span> <span style="color: #339933;">+</span> color <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&quot;&gt;'</span> <span style="color: #339933;">+</span> <span style="color: #000066;">name</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; =&gt; &quot;</span><span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">typeof</span><span style="color: #009900;">&#40;</span>val<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span><span style="color: #3366CC;">'( &lt;/font&gt;'</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">typeof</span> val <span style="color: #339933;">==</span> <span style="color: #3366CC;">'object'</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                ret <span style="color: #339933;">+=</span> <span style="color: #3366CC;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #339933;">+</span><span style="color: #000066;">print</span><span style="color: #009900;">&#40;</span>val<span style="color: #339933;">,</span>i<span style="color: #339933;">+</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                ret <span style="color: #339933;">+=</span> space<span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">typeof</span> val <span style="color: #339933;">==</span> <span style="color: #3366CC;">'function'</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                ret <span style="color: #339933;">+=</span> <span style="color: #3366CC;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span> <span style="color: #339933;">+</span> space <span style="color: #339933;">+</span> indent <span style="color: #339933;">+</span> val.<span style="color: #660066;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/\n/g</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #339933;">+</span>space<span style="color: #339933;">+</span>indent<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #339933;">+</span>space<span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span>
                ret <span style="color: #339933;">+=</span> val <span style="color: #339933;">+</span> <span style="color: #3366CC;">' '</span> <span style="color: #339933;">;</span>
            ret <span style="color: #339933;">+=</span> <span style="color: #3366CC;">'&lt;font style=&quot;color:'</span> <span style="color: #339933;">+</span> color <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&quot;&gt;'</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">')&lt;/font&gt;<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000066; font-weight: bold;">return</span> ret<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">typeof</span> o <span style="color: #339933;">==</span> <span style="color: #3366CC;">'object'</span> <span style="color: #009900;">&#41;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">'&lt;pre&gt;'</span><span style="color: #339933;">+</span><span style="color: #000066;">print</span><span style="color: #009900;">&#40;</span>o<span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">'&lt;/pre&gt;'</span><span style="color: #339933;">;</span>  <span style="color: #006600; font-style: italic;">//use pre to prevent structure destruction</span>
    <span style="color: #000066; font-weight: bold;">else</span>
        <span style="color: #000066; font-weight: bold;">return</span> o<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://pwwang.com/2009/10/27/print_r-in-javascript/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Tetris(俄罗斯方块) in jQuery/JavaScript!</title>
		<link>http://pwwang.com/2009/10/25/tetris-in-jquery-javascript/</link>
		<comments>http://pwwang.com/2009/10/25/tetris-in-jquery-javascript/#comments</comments>
		<pubDate>Sat, 24 Oct 2009 18:09:48 +0000</pubDate>
		<dc:creator>pwwang</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Tetris]]></category>

		<guid isPermaLink="false">http://pwwang.com/?p=746</guid>
		<description><![CDATA[&#160;

All in jQuery/JavaScript + HTML!

Demo：Tetris(俄罗斯方块) in jQuery/JavaScript!

<span class="readmore"><a href="http://pwwang.com/2009/10/25/tetris-in-jquery-javascript/" title="Tetris(俄罗斯方块) in jQuery/JavaScript!">Read More: 2427 Words Totally</a></span>]]></description>
			<content:encoded><![CDATA[<p>&nbsp;</p>
<p>All in jQuery/JavaScript + HTML!</p>
<p>Demo：<a target="_blank" href="http://pwwang.com/tools/tetris.html">Tetris(俄罗斯方块) in jQuery/JavaScript!</a></p>
<p>文章最后会给出源码下载。</p>
<p>先上两张图：</p>
<p><a href="http://pwwang.com/tools/tetris.html" target="_blank"><img border="0" style="clear: both;" src="http://pwwang.com/wp-content/uploads/tetris/Tetris_ready.png" alt="" /></a></p>
<p><a href="http://pwwang.com/tools/tetris.html" target="_blank"><img border="0" style="clear: both;" src="http://pwwang.com/wp-content/uploads/tetris/Tetris_running.png" alt="" /></a></p>
<p><span id="more-746"></span></p>
<p>其实很早就有这样的想法，无奈思想一直不是很成熟，各个细节没有想好。突然某一天灵光闪现，就想到办法了。相信应该也有很多人有过这样的想法，可是总是会被一些细节问题所卡住。只要思路有了，代码的实现上并不难。所以我重点说一下思路。</p>
<p>先说一下这样程序的功能吧。图里面能够看得很清楚了。基本的功能就不说了，其它的包括下一个下落形状的预览（左上角的小方块中），隐藏、显示网格线，隐藏、显示下落时对好的阴影形状。其它的就是俄罗斯方块的基本功能了。</p>
<p>很重要的一点，很多人可能受到了思维定势的影响，觉得既然是俄罗斯方块，那当然是一个个方块往下落。如果你这样想，我敢说这样就算程序的功能都可以完成，也绝对在1000行以上（我的程序不超过1000行，包括各个函数的注释），甚至要远远超过这个数。如果跳出了这个方块下落和思维定势，那问题就好解决了。</p>
<p>有人可能要问了，既然方块不动，那是什么在动呢？有人说颜色，但更确切地说是样式！每一次，让下一个方格的样式用上一个方块来替代，这样下落就成了对样式的操作了！虽然下落是位置在变，位置（top,left）也是样式，但是这可能要涉及到HTML，或者DOM元素的操作了。相比而言，前一种思路不用动任何的HTML元素！这是简化的最关键所在了。</p>
<p>再说一下其它方面。</p>
<p>方块的排列：</p>
<p>用一个float:left就能排列好了，需要注意的是，在设置容器的长宽时，要注意小方块的方框宽度，这个也要计算在内。</p>
<p>按键的处理：</p>
<p>我们知道，像DIV，SPAN（容器用的DIV，方块用的SPAN）无法直接响应按键按下的事件（onkeydown），那我们只好先用可以响应的元素来接收这个事件，我用的是document，由于我把它写在了类的内部，所以，要把事件内容的this变成实际要处理的元素（Tetris），就需要用到javascript的call函数了。</p>
<p>形状在移动（左、右、下、旋转）的时候出界或者是否压住了其他方块的判断：</p>
<p>有两种方式，第一种是先计算好方块可以移动的最小距离，然后再判断方块是否移动超过了这个最小距离。（注意我这里说的移动都是对样式的操作）。这种方式比较麻烦，因为首先要得到方块的最左，最右和最下的方块，然后再通过这些方块来计算这个最小距离，并且旋转就无法用距离来衡量了。但是如果要得到阴影（后面解释阴影的实现）的话，还是得计算这个最小距离。我的做法是移动一步（包括旋转）采取第二种方法，而直接下落和生成阴影而采用第一种方法。</p>
<p>第二种方式是先将坐标值预存起来，然后假设方块移动了，再判断这个方块是否在界内，并没在压住其它的方块。如果不符合条件，再将坐标值还原回去。这里插一句，如果预存坐标？我的坐标采用的是数组的形式储存的，不能直接用＝来赋值，这个只能将变量指向右值而成为右值的引用。要复制数组或对象，必须采用循环或者遍历对象属性，且元素或属性的值为基本类型时才能赋值。</p>
<p>下一个形状预览的问题：</p>
<p>想清楚了最关键的问题，这个问题很好解决，这个就是大容器的缩小版，当前显示形状的时候同时计算下一个形状的序列号，并存储。下次显示形状的时候调用这个序列号就行了。</p>
<p>最后说一下旋转的问题：</p>
<p>其实这是一个数学问题。</p>
<p><img border="0" alt="" src="http://pwwang.com/wp-content/uploads/tetris/rotate.gif" style="clear: both;" /></p>
<p>由于旋转前后，向量的长度（设为R）不变，所以：</p>
<p>cosA = x0/R,&nbsp; sinA = y0/R&nbsp;&nbsp;&nbsp;&nbsp; &#8230;&#8230; &nbsp; （2）</p>
<p>对于右图：</p>
<p>x1 = R * cos（A+B）＝R（cosAcosB &#8211; sinAsinB）</p>
<p>y1 = R|* sin（A+B）＝R（sinAcosB + cosAsinB）</p>
<p>将（2）代入得到：</p>
<p>x1 = x0 * cosB &#8211; y0 * sinB</p>
<p>y1 = x0 * sinB + y0 * cosB</p>
<p>由于我设定的是逆时针旋转，所以再将B＝-B代入上式：</p>
<p>x1 = x0 * cosB + y0 * sinB</p>
<p>y1 = -x0 * sinB + y0 * cosB</p>
<p>再者，我们旋转的角度为90度，再代入，得到：</p>
<p>x1=y0, y1=-x0&nbsp;&nbsp; &#8230;&#8230;&#8230;&#8230;. （3）</p>
<p>这是以原点为出发的向量，如果不是原点为出发点，假设这个点（旋转中心）为（cx，cy），将向量进行平移：</p>
<p>x1-cx = y0-cy, y1-cy = cx-x0，于是（3）变成：</p>
<p>x1 = cx-cy+y0, y1 = cx+cy-x0&nbsp; （4）</p>
<p>这里值得提一下的是，尽管我们的坐标系和正规的xy平面坐标系不同，我们的原点在左上角，x（二维数组第一下标）轴垂直向下，y（二维数组第二下标）轴水平向右，但是由于我们旋转的时候选取的是向量，它的方向是确定的，不随坐标系的变化而变化，所以（4）仍然适用于我们的坐标系。</p>
<p>有了公式（4）,选取适当的旋转中心（x0,y0）就可以进行转换了。为了效率，我们可以把由四个小方块组成的大方块排除在外，不用旋转。</p>
<p><a href="http://pwwang.com/wp-content/uploads/tetris/tetris.rar" target="_blank">源代码下载</a></p>
<p>&nbsp;</p>
<p>Home Page in Google Code: <a href="http://code.google.com/p/tetris-in-jquery-javascript/" target="_blank">http://code.google.com/p/tetris-in-jquery-javascript/</a></p>
<h1>Introduction</h1>
<p>jQuery.msgbox game called tetris(俄罗斯方块) in jQuery/JavaScript<a href="/p/tetris-in-jquery-javascript/w/edit/JavaScript">?</a></p>
<h1>Information</h1>
<p>pwwang <a rel="nofollow" href="http://pwwang.com">http://pwwang.com</a></p>
<p>page <a rel="nofollow" href="http://pwwang.com/technology/js-javascript/tetris-in-jquery-%0A%0Ajavascript/">http://pwwang.com/technology/js-javascript/tetris-in-  jquery-javascript/</a></p>
<p>demo <a rel="nofollow" href="http://pwwang.com/tools/tetris.html">http://pwwang.com/tools/tetris.html</a></p>
<h1>Details</h1>
<ul>
<li>It&#8217;s a classic tetris</li>
<li>It has basic functions of tetris</li>
<li>You could decide to show the grids or not</li>
<li>You could choose to show the shadow( will the falling shape will   locate ) or  not</li>
<li>Pause or resume game</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://pwwang.com/2009/10/25/tetris-in-jquery-javascript/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>jquery.msgbox is now hosting in Google Code</title>
		<link>http://pwwang.com/2009/10/22/jquery-msgbox-is-now-hosting-in-google-code/</link>
		<comments>http://pwwang.com/2009/10/22/jquery-msgbox-is-now-hosting-in-google-code/#comments</comments>
		<pubDate>Thu, 22 Oct 2009 13:02:22 +0000</pubDate>
		<dc:creator>pwwang</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[div提示框]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://pwwang.com/?p=735</guid>
		<description><![CDATA[&#160;

Introduction



<span class="readmore"><a href="http://pwwang.com/2009/10/22/jquery-msgbox-is-now-hosting-in-google-code/" title="jquery.msgbox is now hosting in Google Code">Read More: 712 Words Totally</a></span>]]></description>
			<content:encoded><![CDATA[<p>&nbsp;</p>
<h1>Introduction</h1>
<p>
jQuery.msgbox Popups a msgbox on the web pages using jquery JavaScript Library</p>
<h1>Information</h1>
<p>
pwwang http://pwwang.com</p>
<p>plugin page http://pwwang.com/technology/js-javascript/jquery%e6%8f%92%e4%bb%b6%ef%bc%9adiv%e6%8f%90%e7%a4%ba%e6%a1%86jquery-msgbox-2-0/</p>
<h1>Details</h1>
<p>
It can:</p>
<ul>
<li>simulate window.alert, and the title of the box will flash if you try to click other part of the page</li>
<li>simulate window.confirm</li>
<li>allow you to popup a plain text box</li>
<li>allow you to popup a iframe in the box</li>
<li>allow you to popup a box with Ajax method</li>
<li>be closed automatically as you wish</li>
<li>mask the back contents of page when box showed</li>
<li>do an event you designated when the box closes</li>
<li>be dragged to anywhere in the window as you wish</li>
<li>decided whether popup another box if the page is in the iframe of a box</li>
</ul>
<p>&nbsp;<span style="color: #ff6600"><strong>Project Home Page:</strong></span> <a href="http://code.google.com/p/jquery-msgbox/">http://code.google.com/p/jquery-msgbox/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://pwwang.com/2009/10/22/jquery-msgbox-is-now-hosting-in-google-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>DIV提示框：jquery.msgbox.3.1</title>
		<link>http://pwwang.com/2009/10/12/div%e6%8f%90%e7%a4%ba%e6%a1%86%ef%bc%9ajquery-msgbox-3-1/</link>
		<comments>http://pwwang.com/2009/10/12/div%e6%8f%90%e7%a4%ba%e6%a1%86%ef%bc%9ajquery-msgbox-3-1/#comments</comments>
		<pubDate>Mon, 12 Oct 2009 14:24:25 +0000</pubDate>
		<dc:creator>pwwang</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[div提示框]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://pwwang.com/?p=700</guid>
		<description><![CDATA[&#160;

jquery.msgbox 3.1&#160;&#160;&#160; 历史版本：&#160; 1.0&#160;&#160; 2.0&#160;&#160;3.0

3.1 更新：

<span class="readmore"><a href="http://pwwang.com/2009/10/12/div%e6%8f%90%e7%a4%ba%e6%a1%86%ef%bc%9ajquery-msgbox-3-1/" title="DIV提示框：jquery.msgbox.3.1">Read More: 146 Words Totally</a></span>]]></description>
			<content:encoded><![CDATA[<p>&nbsp;</p>
<p>jquery.msgbox 3.1&nbsp;&nbsp;&nbsp; 历史版本：&nbsp; <a target="_blank" href="http://pwwang.com/technology/js-javascript/%e5%8f%88%e8%a7%81div%e6%8f%90%e7%a4%ba%e6%a1%86jquery%e7%89%88/">1.0</a>&nbsp;&nbsp; <a target="_blank" href="http://pwwang.com/technology/js-javascript/jquery%e6%8f%92%e4%bb%b6%ef%bc%9adiv%e6%8f%90%e7%a4%ba%e6%a1%86jquery-msgbox-2-0/">2.0</a>&nbsp;&nbsp;<a target="_blank" href="http://pwwang.com/technology/js-javascript/div提示框：jquery-msgbox-3-0/">3.0</a></p>
<p><strong>3.1 更新：</strong></p>
<blockquote>
<p>1. 修正IE8以下&ldquo;无法得到 display 属性。 参数无效&rdquo;的bug</p>
<p>2. 关闭提示框后移动提示框的HTML代码</p>
</blockquote>
<p><strong>下载：</strong></p>
<p><a target="_blank" href="/wp-content/uploads/2009/10/jquery.msgbox.3.1/jquery.msgbox.js">jquery.msgbox.js</a></p>
<p><a target="_blank" href="/wp-content/uploads/2009/10/jquery.msgbox.3.1/jquery.msgbox.css">jquery.msgbox.css</a></p>
<p><a target="_blank" href="/wp-content/uploads/2009/10/jquery.msgbox.3.1/close.gif">close.gif</a></p>
]]></content:encoded>
			<wfw:commentRss>http://pwwang.com/2009/10/12/div%e6%8f%90%e7%a4%ba%e6%a1%86%ef%bc%9ajquery-msgbox-3-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

