<?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>haojii &#187; JavaScript</title>
	<atom:link href="http://www.haojii.com/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.haojii.com</link>
	<description>一个专注于技术的IT男</description>
	<lastBuildDate>Thu, 19 Apr 2012 07:58:49 +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>如何写出可维护的面向对象的JavaScript代码?</title>
		<link>http://www.haojii.com/2011/07/javascript-oo/</link>
		<comments>http://www.haojii.com/2011/07/javascript-oo/#comments</comments>
		<pubDate>Sat, 23 Jul 2011 08:46:30 +0000</pubDate>
		<dc:creator>Jacky</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[OO]]></category>

		<guid isPermaLink="false">http://www.haojii.com/?p=598</guid>
		<description><![CDATA[这是个问题，平常我也没思考过，被问到了，一时也没理清楚自己的思路，当时只蹦出namespace这么个想法； 我平时的工作跟javascript打交道的时候一般是做一些页面里的交互体验增强，基本上全是用jQuery，查找一些元素，做些计算，做些属性更改，做一些事件绑定处理，做些AJAX； 数据模型也就是定义JSON对象，其实用javascript OO定义前端的model的情况比较少啦； 这两天我读了网上的一些文章，今天稍微整理一下关于这个问题的多个角度。 1. Object Instantiation 2. Prototype Inheritence 3. Encapsulation(Public/Private) OO面向对象的特性之一，封装，javascript的也能搞定，不酷，甚至有点恶心 4. Namespace 学习java的名称空间，javascript里一直没有标准的做法，从功能上当然是可行的 下面这段是从《JavaScript DOM 高级程序设计》中摘的，貌似更高级一点 5. Module (commonJS) javascript越来越流行，从前几年的AJAX，到现在的nodeJS，前端MVC等等， 腾讯做的WebQQ也是这一范畴，或者还有360桌面，网页和App的界限越来越模糊， 甚至有完全基于浏览器的ChromOS的硬件 这些都说明，javascript天降大任，虽然javascript先天发育得不好。。。 commonJS其实是个规范,而nodeJS是这个规范的实现之一，它的目标是做一个javascript标准库(就像JDK，ruby Gem一样)，借助javascript引擎(eg: V8)，使得javascript也能处理更多的应用场景，学习其他语言成熟的模块化当然是可行的。 当然，自己实现javascript模块的时候，从逻辑上和物理上区分也是不错的实践方法。 6. Module load (requireJS) javscript的库其实很多，有非常成功的jQuery,prototype,YUI等等,由于global对象的关系，不同的库存在对象的冲突， 这时候就需要模块加载机制，管理很多js文件，避免冲突等等，requireJS是用的比较多的一个加载库，google其实也有。 参考资料: Private Members &#8230; <a href="http://www.haojii.com/2011/07/javascript-oo/">继续阅读 <span class="meta-nav">&#8594;</span></a><table class="wumii-related-items" cellspacing="0" cellpadding="2" border="0" width="100%" style="clear: both;">
    
    <tr>
        <td ><b><font size="-1" color="#FF0000" style="display: block !important; padding: 20px 0 5px !important;">其他文章：</font></b></td>
    </tr>
    
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fwww.haojii.com%2F2009%2F12%2Fjavascript-lyrics-show%2F&from=http%3A%2F%2Fwww.haojii.com%2F2011%2F07%2Fjavascript-oo%2F">
                        <font size="-1" color="#667FFA" style="line-height: 1.65em; font-size: 12px !important;">javascript-lyrics-show</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fwww.haojii.com%2F2011%2F03%2Fjavascript-tools%2F&from=http%3A%2F%2Fwww.haojii.com%2F2011%2F07%2Fjavascript-oo%2F">
                        <font size="-1" color="#667FFA" style="line-height: 1.65em; font-size: 12px !important;">记录最近用到的几个JavaScript文件</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fwww.haojii.com%2F2010%2F04%2F20-helpful-jquery-methods-you-should-be-using%2F&from=http%3A%2F%2Fwww.haojii.com%2F2011%2F07%2Fjavascript-oo%2F">
                        <font size="-1" color="#667FFA" style="line-height: 1.65em; font-size: 12px !important;">你应该使用的20个有用的jQuery方法</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fwww.haojii.com%2F2010%2F05%2Fcool-demo-with-jquery-plugin-fancybox%2F&from=http%3A%2F%2Fwww.haojii.com%2F2011%2F07%2Fjavascript-oo%2F">
                        <font size="-1" color="#667FFA" style="line-height: 1.65em; font-size: 12px !important;">Cool demo with jQuery plugin FancyBox</font>
                    </a>
                </td>
            </tr>
    
    <tr>
        <td  align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></description>
			<content:encoded><![CDATA[<p>这是个问题，平常我也没思考过，被问到了，一时也没理清楚自己的思路，当时只蹦出namespace这么个想法；</p>
<p>我平时的工作跟javascript打交道的时候一般是做一些页面里的交互体验增强，基本上全是用jQuery，查找一些元素，做些计算，做些属性更改，做一些事件绑定处理，做些AJAX；</p>
<p>数据模型也就是定义JSON对象，其实用javascript OO定义前端的model的情况比较少啦；</p>
<p>这两天我读了网上的一些文章，今天稍微整理一下关于这个问题的多个角度。</p>
<h2>1. Object Instantiation</h2>
<pre class="brush: jscript; title: ; notranslate">
// 基础，不多解释了
var People = function People(firstName,lastName) {
     this.firstName = firstName;
     this.lastName = lastName;

     this.getName = function() {
          return this.lastName+&quot; &quot;+this.firstName;     // chinese way
     }
}
</pre>
<h2>2. Prototype Inheritence</h2>
<pre class="brush: jscript; title: ; notranslate">
// 常规写法, 当然还有些其他写法, 例如参考2里的Object.create, 但是本质是一样的啦
var Employee = function Employee(firstName, lastName, salary) {
     People.call(this,firstName,lastName);
     this.salary = salary;
}
Employee.prototype = new People();
</pre>
<h2>3. Encapsulation(Public/Private)</h2>
<p>OO面向对象的特性之一，封装，javascript的也能搞定，不酷，甚至有点恶心</p>
<pre class="brush: jscript; title: ; notranslate">
Public

     function Constructor(...) {
          this.membername = value;
     }
     Constructor.prototype.membername = value;

Private

     function Constructor(...) {
          var that = this;
          var membername = value;
          function membername(...) {...}
     }

     Note: The function statement

          function membername(...) {...}

     is shorthand for

          var membername = function membername(...) {...};

Privileged

     function Constructor(...) {
          this.membername = function (...) {...};
     }
</pre>
<h2>4. Namespace</h2>
<p>学习java的名称空间，javascript里一直没有标准的做法，从功能上当然是可行的</p>
<pre class="brush: jscript; title: ; notranslate">
var myNamespace = {
    foo: function() {
    },
    bar: function() {
    }
};
myNamespace.foo();
</pre>
<p>下面这段是从《JavaScript DOM 高级程序设计》中摘的，貌似更高级一点</p>
<pre class="brush: jscript; title: ; notranslate">
(function() {
     // 通过 ID 获取 Element 对象
     function $(id) {
          return document.getElementById(id);
     }

     // 显示对应 ID 的对象的节点类型名字
     function myMethod(id) {
          alert($(id).nodeName);
     }

     // 这是对外的接口
     window['myNamespace']= {}
     window['myNameSpace']['$'] = $;
})();
</pre>
<h2>5. Module (commonJS)</h2>
<p>javascript越来越流行，从前几年的AJAX，到现在的nodeJS，前端MVC等等，<br />
腾讯做的WebQQ也是这一范畴，或者还有360桌面，网页和App的界限越来越模糊，<br />
甚至有完全基于浏览器的ChromOS的硬件</p>
<p>这些都说明，javascript天降大任，虽然javascript先天发育得不好。。。<br />
commonJS其实是个规范,而nodeJS是这个规范的实现之一，它的目标是做一个javascript标准库(就像JDK，ruby Gem一样)，借助javascript引擎(eg: V8)，使得javascript也能处理更多的应用场景，学习其他语言成熟的模块化当然是可行的。</p>
<p>当然，自己实现javascript模块的时候，从逻辑上和物理上区分也是不错的实践方法。</p>
<h2>6. Module load (requireJS)</h2>
<p>javscript的库其实很多，有非常成功的jQuery,prototype,YUI等等,由于global对象的关系，不同的库存在对象的冲突，<br />
这时候就需要模块加载机制，管理很多js文件，避免冲突等等，requireJS是用的比较多的一个加载库，google其实也有。</p>
<h2>参考资料:</h2>
<ol>
<li><a href="http://javascript.crockford.com/private.html">Private Members in JavaScript</a></li>
<li><a href="http://javascript.crockford.com/prototypal.html">Prototypal Inheritance in JavaScript</a></li>
<li><a href="http://msdn.microsoft.com/en-us/scriptjunkie/gg602402">How to Write Maintainable OO JavaScript Code</a></li>
<li><a href="http://dancewithnet.com/2009/03/29/douglas-crockford/">JavaScript大牛：Douglas Crockford</a></li>
</ol>
<table class="wumii-related-items" cellspacing="0" cellpadding="2" border="0" width="100%" style="clear: both;">
    
    <tr>
        <td ><b><font size="-1" color="#FF0000" style="display: block !important; padding: 20px 0 5px !important;">其他文章：</font></b></td>
    </tr>
    
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fwww.haojii.com%2F2009%2F12%2Fjavascript-lyrics-show%2F&from=http%3A%2F%2Fwww.haojii.com%2F2011%2F07%2Fjavascript-oo%2F">
                        <font size="-1" color="#667FFA" style="line-height: 1.65em; font-size: 12px !important;">javascript-lyrics-show</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fwww.haojii.com%2F2011%2F03%2Fjavascript-tools%2F&from=http%3A%2F%2Fwww.haojii.com%2F2011%2F07%2Fjavascript-oo%2F">
                        <font size="-1" color="#667FFA" style="line-height: 1.65em; font-size: 12px !important;">记录最近用到的几个JavaScript文件</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fwww.haojii.com%2F2010%2F04%2F20-helpful-jquery-methods-you-should-be-using%2F&from=http%3A%2F%2Fwww.haojii.com%2F2011%2F07%2Fjavascript-oo%2F">
                        <font size="-1" color="#667FFA" style="line-height: 1.65em; font-size: 12px !important;">你应该使用的20个有用的jQuery方法</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fwww.haojii.com%2F2010%2F05%2Fcool-demo-with-jquery-plugin-fancybox%2F&from=http%3A%2F%2Fwww.haojii.com%2F2011%2F07%2Fjavascript-oo%2F">
                        <font size="-1" color="#667FFA" style="line-height: 1.65em; font-size: 12px !important;">Cool demo with jQuery plugin FancyBox</font>
                    </a>
                </td>
            </tr>
    
    <tr>
        <td  align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></content:encoded>
			<wfw:commentRss>http://www.haojii.com/2011/07/javascript-oo/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>自定义input type=”file”的样式以及引出的相关技术问题</title>
		<link>http://www.haojii.com/2011/04/jquery-change-event-to-input-file-on-ie/</link>
		<comments>http://www.haojii.com/2011/04/jquery-change-event-to-input-file-on-ie/#comments</comments>
		<pubDate>Fri, 29 Apr 2011 06:58:57 +0000</pubDate>
		<dc:creator>Jacky</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[自定义input type=file样式]]></category>

		<guid isPermaLink="false">http://www.haojii.com/?p=552</guid>
		<description><![CDATA[因为不同浏览器下如果使用默认的input type=”file”元素，效果如下 为了保证不同浏览器下的UI一致我们有需求要自定义input type=”file”元素的样式 如何Style input type=”file”元素的相关长篇讨论 1. Styling an input type=”file” http://www.quirksmode.org/dom/inputfile.html 2. Howto Style the Button of a input type=”file” Control http://imar.spaanjaars.com/154/howto-style-the-button-of-a-input-typefile-control 翻译成白话文就是 1. 隐藏input type=”file” 2. 用CSS将a+span做成按钮样式，对其做事件绑定，点击之后触发input type=”file”的点击事件 3. 用input type=”text”显示input type=”file”选中文件的值 查看能工作的live code http://jsfiddle.net/jihao/sFCQV/ HTML CSS &#8230; <a href="http://www.haojii.com/2011/04/jquery-change-event-to-input-file-on-ie/">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>因为不同浏览器下如果使用默认的input type=”file”元素，效果如下</p>
<p><a rel="attachment wp-att-553" href="http://www.haojii.com/2011/04/jquery-change-event-to-input-file-on-ie/filefields/"><img class="aligncenter size-full wp-image-553" title="filefields" src="http://www.haojii.com/blog/wp-content/uploads/filefields.gif" alt="" width="381" height="225" /></a></p>
<p>为了保证不同浏览器下的UI一致我们有需求要自定义input type=”file”元素的样式</p>
<p>如何Style input type=”file”元素的相关长篇讨论<br />
1. Styling an input type=”file”<br />
<a href="http://www.quirksmode.org/dom/inputfile.html">http://www.quirksmode.org/dom/inputfile.html</a></p>
<p>2. Howto Style the Button of a input type=”file” Control<br />
<a href="http://imar.spaanjaars.com/154/howto-style-the-button-of-a-input-typefile-control">http://imar.spaanjaars.com/154/howto-style-the-button-of-a-input-typefile-control</a></p>
<p>翻译成白话文就是<br />
1. 隐藏input type=”file”<br />
2. 用CSS将a+span做成按钮样式，对其做事件绑定，点击之后触发input type=”file”的点击事件<br />
3. 用input type=”text”显示input type=”file”选中文件的值</p>
<p>查看能工作的live code<br />
<a href="http://jsfiddle.net/jihao/sFCQV/">http://jsfiddle.net/jihao/sFCQV/</a></p>
<p>HTML</p>
<pre class="brush: xml; title: ; notranslate">
&lt;input type=&quot;text&quot; id=&quot;fakeFile&quot; name=&quot;fakeFile&quot;&gt;

&lt;a class=&quot;linkButton&quot; id=&quot;link_browse&quot; href=&quot;#&quot;&gt;&lt;span&gt;Link Button BROWSE&lt;/span&gt;&lt;/a&gt;

&lt;input id=&quot;theFile&quot; type=&quot;file&quot;
  style=&quot;visibility:hidden;opacity:0;cursor:pointer;*filter: alpha(opacity=0);&quot;&gt;
</pre>
<p>CSS</p>
<pre class="brush: css; title: ; notranslate">
a.linkButton{
    background: #332233;/*style this link to look like a button*/
    color: #FFFFFF;
    padding: 0 0 0 10px;
    text-decoration: none;
}
a.linkButton&gt;span{
    padding: 0 10px 0 0;
}
</pre>
<p>JavaScript</p>
<pre class="brush: jscript; title: ; notranslate">
$(function() {
    $('#browse,#link_browse').click(function(event) {
        event.preventDefault();
        $('#theFile').trigger('click');
    });

    function changeCallBack() {
        $('#fakeFile').val($('#theFile').val());
    };

    //this works for FF, easy job
    $('#theFile').bind('change', function() {
        alert('gotcha!');
        changeCallBack();

    });
});
</pre>
<p>写完这段代码之后遇到的相关问题<br />
问题1. FireFox下不会显示文件全路径（google得知这是FF的安全考虑，不影响form提交）<br />
问题2. IE下选择文件后不会触发change事件，原因是IE处理打开文件的机制（参考下面的链接看更多讨论）<br />
对于IE8，这个问题的解决办法如下，setTimeout会阻塞，直到文件选择器关闭之后，回调函数才被调用（更新用来显示的input type=”text”的值）</p>
<pre class="brush: jscript; title: ; notranslate">
    if ($.browser.msie) {
        $('#theFile').bind('click', function() {
            //this solution works for IE8, not Maxthon2, so seems not IE6 either(not tested)
            setTimeout(function() {
                alert('[IE8] setTimeout get called after open file dialog was closed');
                changeCallBack();
            }, 0);

        });
    }
</pre>
<p>查看能工作的live code<br />
<a href="http://jsfiddle.net/jihao/sFCQV/">http://jsfiddle.net/jihao/sFCQV/</a></p>
<p>关于IE change事件的相关讨论<br />
1. jQuery, .change() notification, and IE （关于radio button的change事件绑定）<br />
<a href="http://norman.walsh.name/2009/03/24/jQueryIE">http://norman.walsh.name/2009/03/24/jQueryIE</a></p>
<p>2. jQuery: change event to input file on IE<br />
<a href="http://stackoverflow.com/questions/2389341/jquery-change-event-to-input-file-on-ie">http://stackoverflow.com/questions/2389341/jquery-change-event-to-input-file-on-ie</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.haojii.com/2011/04/jquery-change-event-to-input-file-on-ie/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>使用jQuery UI sortable排序表格行的宽度问题</title>
		<link>http://www.haojii.com/2011/04/jquery-ui-sortable-problem-with-table-tr-row-width/</link>
		<comments>http://www.haojii.com/2011/04/jquery-ui-sortable-problem-with-table-tr-row-width/#comments</comments>
		<pubDate>Mon, 11 Apr 2011 14:57:32 +0000</pubDate>
		<dc:creator>Jacky</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[jquery-ui-sortable]]></category>
		<category><![CDATA[sortable]]></category>

		<guid isPermaLink="false">http://www.haojii.com/?p=532</guid>
		<description><![CDATA[遇到的问题是当我拖拽表格某一行排序的时候，这一行的宽度变得很小，下面是原图以及拖拽时候的难看效果 问题的根源是helper对象，这个拖拽的时候clone出来的对象丢失了样式属性，解决办法在stackoverflow找到 http://stackoverflow.com/questions/1307705/jquery-ui-sortable-with-table-and-tr-width 稍作优化之后的效果 (高亮显示拖拽的目标位置) 最近折腾了不少前端代码，比较耗费脑力，做个jQuery熟练工也没啥坏处。]]></description>
			<content:encoded><![CDATA[<p>遇到的问题是当我拖拽表格某一行排序的时候，这一行的宽度变得很小，下面是原图以及拖拽时候的难看效果</p>
<p><a rel="attachment wp-att-533" href="http://www.haojii.com/2011/04/jquery-ui-sortable-problem-with-table-tr-row-width/table_sort_normal/"><img class="aligncenter size-full wp-image-533" title="table_sort_normal" src="http://www.haojii.com/blog/wp-content/uploads/table_sort_normal.png" alt="" width="220" height="130" /></a><a rel="attachment wp-att-534" href="http://www.haojii.com/2011/04/jquery-ui-sortable-problem-with-table-tr-row-width/table_sort_drag/"></a></p>
<p><a rel="attachment wp-att-534" href="http://www.haojii.com/2011/04/jquery-ui-sortable-problem-with-table-tr-row-width/table_sort_drag/"><img class="aligncenter size-full wp-image-534" title="table_sort_drag" src="http://www.haojii.com/blog/wp-content/uploads/table_sort_drag.png" alt="" width="220" height="120" /></a></p>
<p>问题的根源是helper对象，这个拖拽的时候clone出来的对象丢失了样式属性，解决办法在stackoverflow找到 <a href="http://stackoverflow.com/questions/1307705/jquery-ui-sortable-with-table-and-tr-width">http://stackoverflow.com/questions/1307705/jquery-ui-sortable-with-table-and-tr-width</a></p>
<pre class="brush: jscript; title: ; notranslate">

var fixHelper = function(e, tr) {
 var $originals = tr.children();
 var $helper = tr.clone();
 $helper.children().each(function(index) {
 // Set helper cell sizes to match the original sizes
 $(this).width($originals.eq(index).width())
 });
 return $helper;
 };

$(&quot;table tbody&quot;).sortable({
 helper : fixHelper
 });
</pre>
<p>稍作优化之后的效果 (高亮显示拖拽的目标位置)</p>
<p><a rel="attachment wp-att-537" href="http://www.haojii.com/2011/04/jquery-ui-sortable-problem-with-table-tr-row-width/table_sort_drag_fixed/"><img class="aligncenter size-full wp-image-537" title="table_sort_drag_fixed" src="http://www.haojii.com/blog/wp-content/uploads/table_sort_drag_fixed.png" alt="" width="269" height="158" /></a></p>
<pre class="brush: jscript; title: ; notranslate">
//css
tr.ui-state-highlight td {
	background-color: yellow;
}
//javascript
$(&quot;table tbody&quot;).sortable({
placeholder : &quot;ui-state-highlight&quot;,
opacity: 0.6,
helper : fixHelper,
start : function(e, ui) {
 ui.placeholder.html(ui.helper.html());
},
change : function(event, ui) {
 $('tr.ui-state-highlight').html(ui.helper.html());
}
});
</pre>
<p>最近折腾了不少前端代码，比较耗费脑力，做个jQuery熟练工也没啥坏处。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.haojii.com/2011/04/jquery-ui-sortable-problem-with-table-tr-row-width/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>又发明个轮子-jQuery-plugin-hint</title>
		<link>http://www.haojii.com/2011/03/invent-another-wheel-jquery-plugin-hint/</link>
		<comments>http://www.haojii.com/2011/03/invent-another-wheel-jquery-plugin-hint/#comments</comments>
		<pubDate>Sun, 13 Mar 2011 13:27:46 +0000</pubDate>
		<dc:creator>Jacky</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.haojii.com/?p=513</guid>
		<description><![CDATA[这个插件有人写过了，hint这功能简单，我手痒自己写了一遍 update: 对比了下老外写的，果然写的比我简洁，我刚刚还忘记form提交的情况，现在更新了]]></description>
			<content:encoded><![CDATA[<p>这个插件有人写过了，hint这功能简单，我手痒自己写了一遍</p>
<p>update: 对比了下老外写的，果然写的比我简洁，我刚刚还忘记form提交的情况，现在更新了</p>
<pre class="brush: jscript; title: ; notranslate">

/*
 * jQuery hint plugin
 *
 * Version 0.1
 * @requires jQuery v 1.4
 * @author haojii
 */
/**
 *
 * @description Create a hint for input elements,
 * the hint value is the alt attribute’s value of the input element
 *
 * @example $('input').hint(); &lt;input name=&quot;username&quot; alt=&quot;user name&quot;&gt;
 * @desc see @description
 *
 * @author haojii
 */
(function( $ ){

  $.fn.hint = function() {

    // there's no need to do $(this) because
    // &quot;this&quot; is already a jquery object

    // $(this) would be the same as $($('#element'));
	  var hint_color = '#987';
	  return this.each(function(index){
		  var $input = $(this);
		  var $default_color = $(this).css('color');
		  var $form = $(this.form);

		  if(($(this).val() == '')||($(this).val()==$input.attr('alt')))
		    {
				$input.val($input.attr('alt'));
				$input.css('color',hint_color);
			}
			$input.blur(function(){
		      // console.log($(this).val());
		      // the this keyword is a DOM element

		      //set the hint when user haven't type in anything and left the input field
		      if($(this).val() == $input.attr('alt')
		    		  || $(this).val() == '')
		    	{
		    	  $input.val($input.attr('alt'));
		    	  $input.css('color',hint_color);
		    	}
		    }).focus(function(){
		    	//clear the hint when input field get focus and user haven't type in anything
		    	if($(this).val() == $input.attr('alt'))
		    	{
		    		$input.val('');
		    		$input.css('color',$default_color);
		    	}
		    	//console.log(&quot;focus&quot;+$(this).val());
		    });

			//clear the hint value when form submit
			$form.submit(function(){
				if($input.val() == $input.attr('alt'))
				{
					$input.val('');
				}
			});

	  });

  };
})( jQuery );
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.haojii.com/2011/03/invent-another-wheel-jquery-plugin-hint/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>记录最近用到的几个JavaScript文件</title>
		<link>http://www.haojii.com/2011/03/javascript-tools/</link>
		<comments>http://www.haojii.com/2011/03/javascript-tools/#comments</comments>
		<pubDate>Sat, 05 Mar 2011 15:20:04 +0000</pubDate>
		<dc:creator>Jacky</dc:creator>
				<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://www.haojii.com/?p=510</guid>
		<description><![CDATA[1. Faux Console， 简单的说它是IE下的console.log() http://icant.co.uk/sandbox/fauxconsole/ 2. l10n.js，前端js代码的本地化支持  https://github.com/eligrey/l10n.js/ 3. inspect javascript object http://www.codeproject.com/KB/miscctrl/JS_Inspect_Object.aspx]]></description>
			<content:encoded><![CDATA[<p>1. Faux Console， 简单的说它是IE下的console.log()</p>
<p><a href="http://icant.co.uk/sandbox/fauxconsole/">http://icant.co.uk/sandbox/fauxconsole/</a></p>
<p>2. l10n.js，前端js代码的本地化支持</p>
<p> <a href="https://github.com/eligrey/l10n.js/">https://github.com/eligrey/l10n.js/</a></p>
<p>3. inspect javascript object</p>
<p><a href="http://www.codeproject.com/KB/miscctrl/JS_Inspect_Object.aspx">http://www.codeproject.com/KB/miscctrl/JS_Inspect_Object.aspx</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.haojii.com/2011/03/javascript-tools/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>jQuery Tips : 给AJAX回调函数传递额外参数</title>
		<link>http://www.haojii.com/2010/06/jquery-tips-pass-more-parameters-into-ajax-callback/</link>
		<comments>http://www.haojii.com/2010/06/jquery-tips-pass-more-parameters-into-ajax-callback/#comments</comments>
		<pubDate>Sat, 05 Jun 2010 09:15:07 +0000</pubDate>
		<dc:creator>Jacky</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[web开发]]></category>
		<category><![CDATA[高手必备]]></category>

		<guid isPermaLink="false">http://www.haojii.com/?p=325</guid>
		<description><![CDATA[讨论这个问题基于如下场景：点击页面上某个按钮之后，触发click事件，事件处理函数内部发送一个AJAX请求，AJAX回调函数更新页面的某一个部分 具体到这个例子，我们希望button1和button2点击之后，用AJAX的方式取example.html的内容，然后动态更新页面的id=callbackdemo3的div HTML如下：  第一种做法： 适用于最简单的情况，也是比较直观的做法，就是在ajax回调函数中，使用jQuery的id选择器$(“#callbackdemo3&#8243;)得到id为callbackdemo3的div后更新其HTML内容   第二种做法： 定义好一个接受额外参数的回调函数，然后在默认回调函数的内容调用这个预先定义好的回调函数  这样就能达到传递额外参数的目的了，较之第一种方法，此方法能比较方便的利用各个回调函数的上下文。   对于额外参数的重要性，不妨看一个稍微复杂一点点的HTML情况，我们希望更新button3下面的那个div的HTML，这个div没有id，那么如何做呢？  当然使用#callback_complexdemo&#62;div也是可以的，但是如果是更加复杂的HTML页面呢？如果没有id=callback_complexdemo呢？嵌套很深呢？ 这种情况下，我们就需要有效利用上下文参数这个特性了。 在click的事件处理函数load()中，我们可以方便的获得当前元素的位置var whereAmI = $(this); 这个whereAmI是个jQuery对象，然后在AJAX回调函数f3中，我们利用whereAmI即被点中的这个button想办法去获得想要的div 这一行代码新手有的人看不懂：$(&#8216;div&#8217;,$(whereAmI).closest(&#8216;div&#8217;)) 首先$closest_parent_div = $(whereAmI).closest(&#8216;div&#8217;)即查询获得whereAmI最近的父div对象 然后$(&#8216;div&#8217;,$closest_parent_div) 即在最近的父div对象这个上下文中，查找内部的div对象    希望通过这个小例子，能让大家学会如何给AJAX回调函数传递额外参数，以及实用且重要的上下文参数。]]></description>
			<content:encoded><![CDATA[<p>讨论这个问题基于如下场景：点击页面上某个按钮之后，触发click事件，事件处理函数内部发送一个AJAX请求，AJAX回调函数更新页面的某一个部分</p>
<p>具体到这个例子，我们希望button1和button2点击之后，用AJAX的方式取example.html的内容，然后动态更新页面的id=callbackdemo3的div</p>
<p>HTML如下： </p>
<pre class="brush: xml; title: ; notranslate">
&lt;div id=&quot;callbackdemo1&quot;&gt;
	&lt;button id=&quot;button1&quot;&gt;ajax load1&lt;/button&gt;&lt;br/&gt;
&lt;/div&gt;
&lt;div id=&quot;callbackdemo2&quot;&gt;
	&lt;button id=&quot;button2&quot;&gt;ajax load2&lt;/button&gt;&lt;br/&gt;
&lt;/div&gt;

&lt;div id=&quot;callbackdemo3&quot; class=&quot;log&quot;&gt;&lt;/div&gt;

&lt;button onclick=&quot;$('.log').html('');&quot;&gt;clear&lt;/button&gt;
</pre>
<p><strong>第一种做法：</strong></p>
<p>适用于最<span style="color: #ff0000;">简单</span>的情况，也是比较<span style="color: #ff0000;">直观</span>的做法，就是在ajax回调函数中，使用jQuery的<span style="color: #ff0000;">id选择器</span>$(“#callbackdemo3&#8243;)得到id为callbackdemo3的div后更新其HTML内容</p>
<pre class="brush: jscript; title: ; notranslate">
$(&quot;#callbackdemo1&gt;#button1&quot;).click(
function load(){
	$.get(&quot;example.html&quot;,{ 'param[]': [&quot;var1&quot;, &quot;var2&quot;] },
		function f1(data, textStatus, XMLHttpRequest)
		{
			$(&quot;#callbackdemo3&quot;).html(data);
		}
		);
}
);
</pre>
<p><strong> </strong></p>
<p><strong>第二种做法：</strong></p>
<p>定义好一个接受额外参数的回调函数，然后在默认回调函数的内容调用这个预先定义好的回调函数</p>
<p> 这样就能达到传递额外参数的目的了，较之第一种方法，<span style="color: #ff0000;">此方法能比较方便的利用各个回调函数的上下文</span>。</p>
<pre class="brush: jscript; title: ; notranslate">
function callback_with_extraParam(data,param)
{
	param.html(data);
}
$(&quot;#callbackdemo2&gt;#button2&quot;).click(
function load(){
	var extraParam = $(&quot;#callbackdemo3&quot;)
	$.get(&quot;example.html&quot;,{ 'param[]': [&quot;var1&quot;, &quot;var2&quot;] },
		function f2(data)
		{
			callback_with_extraParam(data,extraParam);
		}
		);
}
);
</pre>
<p> </p>
<p>对于<span style="color: #ff0000;">额外参数的重要性</span>，不妨看一个稍微复杂一点点的HTML情况，我们希望更新button3下面的那个div的HTML，这个div没有id，那么如何做呢？ </p>
<pre class="brush: xml; title: ; notranslate">
&lt;div id=&quot;callback_complexdemo&quot;&gt;
  &lt;button id=&quot;button3&quot;&gt;ajax load3&lt;/button&gt;&lt;br/&gt;
  &lt;button onclick=&quot;$('div',$(this).closest('div')).html('');&quot;&gt;clear&lt;/button&gt;&lt;br/&gt;
  &lt;div&gt;&lt;/div&gt;
&lt;/div&gt;
</pre>
<p>当然使用#callback_complexdemo&gt;div也是可以的，但是如果是更加复杂的HTML页面呢？如果没有id=callback_complexdemo呢？嵌套很深呢？</p>
<p>这种情况下，我们就<span style="color: #ff0000;">需要有效利用上下文参数这个特性</span>了。</p>
<p>在click的事件处理函数<span style="color: #ff0000;">load()</span>中，我们可以方便的获得<span style="color: #ff0000;">当前元素</span>的位置var whereAmI = <span style="color: #ff0000;">$(this);<br />
</span>这个whereAmI是个jQuery对象，然后在AJAX回调函数f3中，我们利用whereAmI即被点中的这个button想办法去获得想要的div</p>
<p>这一行代码新手有的人看不懂：$(&#8216;div&#8217;,$(whereAmI).closest(&#8216;div&#8217;))</p>
<p>首先$closest_parent_div = $(whereAmI).closest(&#8216;div&#8217;)即查询获得whereAmI最近的父div对象</p>
<p>然后$(&#8216;div&#8217;,$closest_parent_div) 即在最近的父div对象这个上下文中，查找内部的div对象</p>
<pre class="brush: jscript; title: ; notranslate">
$(&quot;#callback_complexdemo&gt;#button3&quot;).click(
function load(){
	var whereAmI = $(this);
	$.get(&quot;example.html&quot;,{ 'param[]': [&quot;var1&quot;, &quot;var2&quot;] },
		function f3(data)
		{
			console.log(whereAmI);
			$('div',$(whereAmI).closest('div')).html(data);
		}
		);
}
);
</pre>
<p> </p>
<p> 希望通过这个小例子，能让大家学会如何给AJAX回调函数传递额外参数，以及实用且重要的上下文参数。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.haojii.com/2010/06/jquery-tips-pass-more-parameters-into-ajax-callback/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Cool demo with jQuery plugin FancyBox</title>
		<link>http://www.haojii.com/2010/05/cool-demo-with-jquery-plugin-fancybox/</link>
		<comments>http://www.haojii.com/2010/05/cool-demo-with-jquery-plugin-fancybox/#comments</comments>
		<pubDate>Fri, 21 May 2010 07:40:25 +0000</pubDate>
		<dc:creator>Jacky</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[WebDev]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[web开发]]></category>

		<guid isPermaLink="false">http://www.haojii.com/?p=307</guid>
		<description><![CDATA[$(document).ready(function() { $("#login").fancybox({ 'padding' : 0, 'autoScale' : false, 'transitionIn' : 'none', 'transitionOut' : 'none' }); $("#flash").fancybox({ 'padding' : 0, 'autoScale' : false, 'transitionIn' : 'none', 'transitionOut' : 'none' }); }); 猛击我查看效果 A Flash Demo 代码如下：]]></description>
			<content:encoded><![CDATA[<p><script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script></p>
<p><script type="text/javascript" src="http://www.haojii.com/webdev/fancyboxdemo/fancybox/jquery.mousewheel-3.0.2.pack.js"></script></p>
<p><script type="text/javascript" src="http://www.haojii.com/webdev/fancyboxdemo/fancybox/jquery.fancybox-1.3.1.js"></script></p>
<link rel="stylesheet" type="text/css" href="http://www.haojii.com/webdev/fancyboxdemo/fancybox/jquery.fancybox-1.3.1.css" media="screen" />
<p><script type="text/javascript">
		$(document).ready(function() {
			$("#login").fancybox({
				'padding'			: 0,
				'autoScale'			: false,
				'transitionIn'		: 'none',
				'transitionOut'		: 'none'
			});
			$("#flash").fancybox({
				'padding'			: 0,
				'autoScale'			: false,
				'transitionIn'		: 'none',
				'transitionOut'		: 'none'
			});
		});
	</script></p>
<div>
<h1><a id="login" href="#register-form" title="This is the title for register form">猛击我查看效果</a></h1>
<div style="display:none">
<p><!-- Registration -->  </p>
<div id="register-form" style="width: 500px; height: 300px; margin:auto;">
<h1><a id="flash" href="http://www.adobe.com/jp/events/cs3_web_edition_tour/swfs/perform.swf">A Flash Demo</a></h1>
</p></div>
</p></div>
<p><!-- /Registration -->  </p>
</div>
<p>代码如下：</p>
<pre class="brush: xml; title: ; notranslate">
&lt;script type=&quot;text/javascript&quot; src=&quot;http://code.jquery.com/jquery-1.4.2.min.js&quot;&gt;&lt;/script&gt;

&lt;script type=&quot;text/javascript&quot; src=&quot;http://www.haojii.com/webdev/fancyboxdemo/fancybox/jquery.mousewheel-3.0.2.pack.js&quot;&gt;&lt;/script&gt;

&lt;script type=&quot;text/javascript&quot; src=&quot;http://www.haojii.com/webdev/fancyboxdemo/fancybox/jquery.fancybox-1.3.1.js&quot;&gt;&lt;/script&gt;

&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;http://www.haojii.com/webdev/fancyboxdemo/fancybox/jquery.fancybox-1.3.1.css&quot; media=&quot;screen&quot; /&gt;

&lt;script type=&quot;text/javascript&quot;&gt;
		$(document).ready(function() {
			$(&quot;#login&quot;).fancybox({
				'padding'			: 0,
				'autoScale'			: false,
				'transitionIn'		: 'none',
				'transitionOut'		: 'none'
			});
			$(&quot;#flash&quot;).fancybox({
				'padding'			: 0,
				'autoScale'			: false,
				'transitionIn'		: 'none',
				'transitionOut'		: 'none'
			});
		});
	&lt;/script&gt;

&lt;div&gt;
&lt;h1&gt;&lt;a id=&quot;login&quot; href=&quot;#register-form&quot; title=&quot;This is the title for register form&quot;&gt;猛击我吧!&lt;/a&gt;&lt;/h1&gt;
    &lt;div style=&quot;display:none&quot;&gt; 

&lt;!-- Registration --&gt;
		&lt;div id=&quot;register-form&quot; style=&quot;width: 500px; height: 300px; margin:auto;&quot;&gt;
			&lt;h1&gt;&lt;a id=&quot;flash&quot; href=&quot;http://www.adobe.com/jp/events/cs3_web_edition_tour/swfs/perform.swf&quot;&gt;A Flash Demo&lt;/a&gt;&lt;/h1&gt;
		&lt;/div&gt;
   &lt;/div&gt;
&lt;!-- /Registration --&gt;  

&lt;/div&gt;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.haojii.com/2010/05/cool-demo-with-jquery-plugin-fancybox/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>你应该使用的20个有用的jQuery方法</title>
		<link>http://www.haojii.com/2010/04/20-helpful-jquery-methods-you-should-be-using/</link>
		<comments>http://www.haojii.com/2010/04/20-helpful-jquery-methods-you-should-be-using/#comments</comments>
		<pubDate>Thu, 15 Apr 2010 09:06:33 +0000</pubDate>
		<dc:creator>Jacky</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[web开发]]></category>
		<category><![CDATA[翻译]]></category>

		<guid isPermaLink="false">http://www.haojii.com/?p=276</guid>
		<description><![CDATA[原文地址：20 Helpful jQuery Methods you Should be Using 原文作者：Andrew Burgess 这篇文章介绍了jQuery中有用的20个方法，1-20编号如下，本文没有对照原文逐句翻译，这个20个方法名称，我其实就是用下面第14个map()的方式取出来的，亮点如下，firebug真是个离不开的好工具。 说到工具，那就顺便再提一下一个简洁的jQuery代码调试工具：jQueryPad 支持代码高亮，不支持代码辅助，小问题是这个工具带的jQuery.js的版本旧了，需要自己更新一份到它的目录。 （当然，我还是最喜欢firebug，特别是网页复杂的时候） 1 after() / before() 这个无需多说了，上面的图就是个例子，就是在当前对象的前面或者后面插入内容，被插入的内容是当然对象的兄弟节点 2 change() 和click()或者hover()一样，它也是个事件处理器，这个change事件作用于 textarea, text input, select, 当目标元素的值改变的时候触发该事件。它不同于对象失去焦点时触发的事件foucusOut()和blur()。 change()事件对于客户端验证的任务最适合不过了，因为你不需要在输入字段值没有变化的时候重新验证字段。 3 Context 在jQuery中，使用选择器，其实默认作用于document这个上下文上 了解了这一点话，Context就简单了，它是一个属性兼参数 作为属性：每个jQuery对象，都有个context属性 作为参数：当使用jQuery选择器时，可以使用第二个参数context，细化了查询的范围 用Firebug试验一下就明白了！ 4 data() / removeData() 在元素上（确切的说应该是jQuery对象）上存储数据的一种办法 &#8230; <a href="http://www.haojii.com/2010/04/20-helpful-jquery-methods-you-should-be-using/">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>原文地址：<a href="http://net.tutsplus.com/tutorials/javascript-ajax/20-helpful-jquery-methods-you-should-be-using/">20 Helpful jQuery Methods you Should be Using</a><br />
原文作者：<a title="Andrew Burgess" href="http://net.tutsplus.com/author/andrewburgess/">Andrew Burgess</a></p>
<p>这篇文章介绍了jQuery中有用的20个方法，1-20编号如下，本文没有对照原文逐句翻译，这个20个方法名称，我其实就是用下面第14个map()的方式取出来的，亮点如下，firebug真是个离不开的好工具。</p>
<p><a href="http://www.haojii.com/wp-content/uploads/20jQueryMethods.png"><img class="aligncenter size-full wp-image-277" title="20jQueryMethods" src="http://www.haojii.com/wp-content/uploads/20jQueryMethods.png" alt="" width="806" height="825" /></a></p>
<p>说到工具，那就顺便再提一下一个简洁的jQuery代码调试工具：<a href="http://www.paulstovell.com/jquerypad">jQueryPad</a></p>
<p>支持代码高亮，不支持代码辅助，小问题是这个工具带的jQuery.js的版本旧了，需要自己更新一份到它的目录。</p>
<p>（当然，我还是最喜欢firebug，特别是网页复杂的时候）</p>
<p><a href="http://www.haojii.com/wp-content/uploads/jQueryPad.png"><img class="aligncenter size-full wp-image-278" title="jQueryPad" src="http://www.haojii.com/wp-content/uploads/jQueryPad.png" alt="" width="834" height="595" /></a></p>
<h3>1 after() / before()</h3>
<p>这个无需多说了，上面的图就是个例子，就是在当前对象的前面或者后面插入内容，被插入的内容是当然对象的兄弟节点</p>
<h3>2 change()</h3>
<p>和click()或者hover()一样，它也是个事件处理器，这个change事件作用于 textarea, text input, select, 当目标元素的值改变的时候触发该事件。它不同于对象失去焦点时触发的事件foucusOut()和blur()。</p>
<p>change()事件对于客户端验证的任务最适合不过了，因为你不需要在输入字段值没有变化的时候重新验证字段。</p>
<pre class="brush: jscript; title: ; notranslate">
$(document).ready(
    function() {
$('input[type=text]').change(function () {
    switch (this.id) {
/* some validation code here */
case &quot;name&quot;:
alert(this.value+&quot;....&quot;);
break;
default:
alert(this.id);
break;
    }
});
    });

// HTML Form
Name: &lt;input id=&quot;name&quot; type=&quot;text&quot;/&gt;
</pre>
<h3>3 Context</h3>
<p>在jQuery中，使用选择器，其实默认作用于document这个上下文上</p>
<p>了解了这一点话，Context就简单了，它是一个属性兼参数</p>
<p>作为属性：每个jQuery对象，都有个context属性</p>
<p>作为参数：当使用jQuery选择器时，可以使用第二个参数context，细化了查询的范围</p>
<p>用Firebug试验一下就明白了！</p>
<h3>4 data() / removeData()</h3>
<p>在元素上（确切的说应该是jQuery对象）上存储数据的一种办法</p>
<h3>5 queue() / dequeue()</h3>
<p>用在jQuery动画效果上，可以方便的增加或者移除一个特效</p>
<h3>6 delay()</h3>
<p>暂停一段时间的动画效果</p>
<h3>7 bind(), unbind(),live(), and die()</h3>
<p>以前的文章说过了，不废话了</p>
<h3>8 eq()</h3>
<p>用数组下标的方式取得一组元素中的某一个</p>
<h3>9 get()</h3>
<p>取得jQuery元素对应的Dom对象格式</p>
<h3>10 grep()</h3>
<p>这个我挺喜欢，第二个参数是个用于过滤的回调函数，很容易懂的</p>
<pre class="brush: jscript; title: ; notranslate">
 var nums = '1,2,3,4,5,6,7,8,9,10'.split(',');

 nums = $.grep(nums, function(num, index) {
   // num = the current value for the item in the array
   // index = the index of the item in the array
   return num &gt; 5; // returns a boolean
 });

 console.log(nums) // 6,7,8,9,10
</pre>
<h3>11 Pseudo-Selectors</h3>
<pre class="brush: jscript; title: ; notranslate">
$(':animated'); // returns all elements currently animating
$(':contains(me)'); // returns all elements with the text 'me'
$(':empty'); // returns all elements with no child nodes or text
$(':parent'); // returns all elements with child nodes or text
$('li:even'); // returns all even-index elements (in this case, &lt;li&gt;s)
$('li:odd'); // can you guess?
$(':header'); // returns all h1 - h6s.
$('li:gt(4)'); // returns all elements with an (zero-based) index greater than the given number
$('li:lt(4)'); // returns all element with an index less than the given number
$(':only-child'); // returns all . . . well, it should be obvious
</pre>
<h3>12 isArray() / isEmptyObject() / isFunction() / isPlainObject()</h3>
<pre class="brush: jscript; title: ; notranslate">
 $.isArray([1, 2, 3]); // returns true
 $.isEmptyObject({}); // returns true
 $.isFunction(function () { /****/ }); // returns true

 function Person(name) {
     this.name = name
     return this;
 }
 $.isPlainObject({})); // returns true
 $.isPlainObject(new Object()); // returns true
 $.isPlainObject(new Person()); // returns false
</pre>
<h3>13 makeArray()</h3>
<p>使用jQuery选择器返回的都是jQuery对象，所以$.makeArray()变得挺实用的</p>
<pre class="brush: jscript; title: ; notranslate">
    var ps = $('p');
    $.isArray(ps); //returns false;
    ps = $.makeArray(ps);
    $.isArray(ps); // returns true;
</pre>
<h3>14 map()</h3>
<p>上面firebug的贴图中延伸过了，自己试试看吧</p>
<h3>15 parseJSON()</h3>
<p>把JSON格式的字符串结果解析成JSON对象</p>
<h3>16 proxy()</h3>
<p>$.proxy() 解决了函数调用的上下文问题</p>
<pre class="brush: jscript; title: ; notranslate">
 var person = {
   name : &quot;Andrew&quot;,
   meet : function () {
     alert('Hi! My name is ' + this.name);
   }
 };
 person.meet();   // 正常工作
 $('#test').click(person.meet); // 返回 Hi! My name is undefined 或者 Hi! My name is 空

$('#test').click($.proxy(person.meet, person));
// we could also do $.proxy(person, &quot;meet&quot;)
</pre>
<h3>17 replaceAll() / replaceWith()</h3>
<p>用来替换内容的，replaceWith更顺脑</p>
<pre class="brush: jscript; title: ; notranslate">
&lt;div class=&quot;error1&quot;&gt;error section 1&lt;/div&gt;
&lt;br/&gt;
&lt;div class=&quot;error2&quot;&gt;error section 2&lt;/div&gt;
&lt;br/&gt;
$('&lt;span class=fixed&gt;The error has been corrected&lt;/span&gt;').replaceAll('.error1');
&quot;$('.error2').replaceWith('&lt;span class=fixed&gt;The error has been corrected&lt;/span&gt;');
</pre>
<h3>18 serialize() / serializeArray()</h3>
<p>看例子吧，易懂</p>
<pre class="brush: xml; title: ; notranslate">
 &lt;form&gt;
     &lt;input type=&quot;text&quot; name=&quot;name&quot; value=&quot;John Doe&quot; /&gt;
     &lt;input type=&quot;text&quot; name=&quot;url&quot; value=&quot;http://www.example.com&quot; /&gt;
 &lt;/form&gt;
</pre>
<pre class="brush: jscript; title: ; notranslate">
console.log($('form').serialize());​​​ // logs : name=John+Doe&amp;url=http%3A%2F%2Fwww.example.com
console.log($('form').serializeArray());
// logs : [{ name : 'name', value : 'John Doe'} , { name : 'url', value : 'http://www.example.com' } ]
</pre>
<h3>19 siblings()</h3>
<p>返回所有兄弟节点</p>
<h3>20 wrap() / wrapAll() / wrapInner()</h3>
<p>在作用对象的外面，周围，或者里面，包一层东西</p>
<h3>Conclusion</h3>
<p>好好玩这些个方法吧！</p>
<p>&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.haojii.com/2010/04/20-helpful-jquery-methods-you-should-be-using/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>为iPhone优化网页的技巧</title>
		<link>http://www.haojii.com/2010/02/optimize-web-pages-for-iphone/</link>
		<comments>http://www.haojii.com/2010/02/optimize-web-pages-for-iphone/#comments</comments>
		<pubDate>Sun, 07 Feb 2010 10:05:57 +0000</pubDate>
		<dc:creator>Jacky</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[WebDev]]></category>
		<category><![CDATA[web开发]]></category>
		<category><![CDATA[技巧]]></category>

		<guid isPermaLink="false">http://www.haojii.com/?p=213</guid>
		<description><![CDATA[为iPhone优化网页的技巧,为了使得网页看上去更像native的程序，我们需要对网页进行一些优化

本文包含一些小的技巧：
1. 如何隐藏iPhone Safari的地址栏
2. 如何设置网页的桌面快捷方式图标
3. 如何为网页设置iPhone的样式
4. 如何在网页中检测屏幕的方向
5. 如何在网页中检测当前的位置
 <a href="http://www.haojii.com/2010/02/optimize-web-pages-for-iphone/">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>为了使得网页看上去更像native的程序，我们需要对网页进行一些优化</p>
<p><a href="http://www.haojii.com/webdev/iphone/">使用iPhone访问本文的测试页面</a></p>
<p>本文包含一些小的技巧：<br />
1. 如何隐藏iPhone Safari的地址栏<br />
2. 如何设置网页的桌面快捷方式图标<br />
3. 如何为网页设置iPhone的样式<br />
4. 如何在网页中检测屏幕的方向<br />
5. 如何在网页中检测当前的位置</p>
<p>为iPhone优化网页，最需要了解的应该是下面这个属性<br />
viewport: 可见窗口</p>
<p>可见窗口在iPhone Safari上和桌面Safari略有不同，iPhone上默认没有滚动条，默认的实际的可见大小在竖屏状态下，如下图是320*356，地址栏占据了60像素的高度，稍后我们有办法将其隐藏，得以使用这宝贵的60像素。</p>
<p>iPhone的Safari在显示传统的web网页时（未做优化的网页），viewport会被设置成显示网页宽度的980像素，即在320*356的大小内显示原网页980像素的内容，做了适当的缩放等调整。</p>
<p><a href="http://www.haojii.com/wp-content/uploads/iphone-viewport.png"><img class="aligncenter size-full wp-image-214" title="iphone-viewport" src="http://www.haojii.com/wp-content/uploads/iphone-viewport.png" alt="" width="780" height="600" /></a></p>
<p>所以我们做网页优化的时候，可以通过设置这个meta属性，更多细节可以参考： “Safari Web Content Guide.pdf”</p>
<pre class="brush: xml; title: ; notranslate">
&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width,height=device-height, initial-scale=1.0, user-scalable=no&quot;/&gt;
</pre>
<p>width=device-width, //设备宽度</p>
<p>height=device-height, //设备高度</p>
<p>initial-scale=1.0, //初始的缩放比例</p>
<p>user-scalable=no //是否允许用户缩放</p>
<h3>小技巧</h3>
<h4>1. 如何隐藏iPhone Safari的地址栏</h4>
<p>设置这个属性(文档里面说有用，但是好像不太成功)</p>
<pre class="brush: xml; title: ; notranslate">&lt;meta name=&quot;apple-mobile-web-app-capable&quot; content=&quot;yes&quot; /&gt;</pre>
<p>还有一种办法，页面加载完成之后滚动窗口，这个确实有效，唯一要注意的是页面高度必须够长，我一般设成device-height就没问题了:</p>
<pre class="brush: jscript; title: ; notranslate">
&lt;script type=&quot;text/javascript&quot;&gt;
addEventListener(&quot;load&quot;, function() { setTimeout(hideURLbar, 0); }, false);

function hideURLbar(){
window.scrollTo(0,1);
}
&lt;/script&gt;
</pre>
<h4>2. 如何设置网页的桌面快捷方式图标</h4>
<p>这个其实不算技巧，只是从Apple文档里找到这一项罢了</p>
<pre class="brush: xml; title: ; notranslate">
&lt;link rel=&quot;apple-touch-icon&quot; href=&quot;apple-touch-icon.png&quot;/&gt;
&lt;link rel=&quot;apple-touch-startup-image&quot; href=&quot;startup.png&quot;/&gt;
</pre>
<p>第一行就是设置桌面快捷方式图标的，图标必须是57*57像素的文件，不需要自己做圆角和高亮效果，系统会自动帮你搞定这个<br />
放在网页根目录会作用于下面的全部网页，当然也可以为每个页面设置单独的图标</p>
<p>第二行是设置启动画面（没看到成功效果）</p>
<h4>3. 如何为网页设置iPhone的样式</h4>
<p>这个是CSS media selector的一点小技巧</p>
<pre class="brush: xml; title: ; notranslate">
&lt;!--[if !IE]&gt;--&gt;
&lt;link media=&quot;only screen and (max-device-width: 480px)&quot;
  rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;iphone.css&quot;/&gt;
&lt;!--&lt;![endif]--&gt;
</pre>
<h4>4. 如何在网页中检测屏幕的方向</h4>
<pre class="brush: xml; title: ; notranslate">
&lt;script type=&quot;text/javascript&quot;&gt;
function updateOrientation()
{
	var displayStr = &quot;Orientation : &quot;;
	switch(window.orientation)
	{
	case 0:
		displayStr += &quot;Portrait&quot;;
		break;
	case -90:
		displayStr += &quot;Landscape (right, screen turned clockwise)&quot;;
		break;
	case 90:
		displayStr += &quot;Landscape (left, screen turned counterclockwise)&quot;;
		break;
	case 180:
		displayStr += &quot;Portrait (upside-down portrait)&quot;;
		break;
	}
	document.getElementById(&quot;output&quot;).innerHTML = displayStr;
}
&lt;/script&gt;
&lt;body onorientationchange=&quot;updateOrientation();&quot;&gt;
</pre>
<h4>5. 如何在网页中检测当前的位置</h4>
<pre class="brush: xml; title: ; notranslate">
&lt;script type=&quot;text/javascript&quot;&gt;
navigator.geolocation.getCurrentPosition(showMap);
function showMap(position) {
	latitude = position.coords.latitude;
	longitude = position.coords.longitude;
	document.getElementById(&quot;latitude&quot;).innerHTML = latitude;
	document.getElementById(&quot;longitude&quot;).innerHTML = longitude;
}

//Register for location changes
var watchId = navigator.geolocation.watchPosition(scrollMap,handleError);

function scrollMap(position) {
	latitude = position.coords.latitude;
	longitude = position.coords.longitude;
	document.getElementById(&quot;latitude&quot;).innerHTML = &quot;scrollMap-&gt;&quot;+latitude;
	document.getElementById(&quot;longitude&quot;).innerHTML = &quot;scrollMap-&gt;&quot;+longitude;
}
function handleError(error) {
	document.getElementById(&quot;error&quot;).innerHTML = error;
}

function buttonClickHandler() {
	// Unregister when the user clicks a button
	navigator.geolocation.clearWatch(watchId);
}

&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div id=&quot;location&quot;&gt;
	latitude:&lt;p id=&quot;latitude&quot;&gt;null&lt;/p&gt;
	longitude:&lt;p id=&quot;longitude&quot;&gt;null&lt;/p&gt;
&lt;/div&gt;
&lt;div&gt;
	&lt;input type=&quot;button&quot; value=&quot;clearWatch&quot; onclick=&quot;buttonClickHandler();&quot;/&gt;
&lt;/div&gt;
&lt;div id=&quot;error&quot;&gt;&lt;/div&gt;
&lt;/body&gt;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.haojii.com/2010/02/optimize-web-pages-for-iphone/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>使用AJAX集成Gravatar头像到自己的评论系统</title>
		<link>http://www.haojii.com/2010/01/integrate-gravatar-for-comment-using-ajax/</link>
		<comments>http://www.haojii.com/2010/01/integrate-gravatar-for-comment-using-ajax/#comments</comments>
		<pubDate>Wed, 20 Jan 2010 07:31:38 +0000</pubDate>
		<dc:creator>Jacky</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[WebDev]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[comment]]></category>
		<category><![CDATA[gravatar]]></category>
		<category><![CDATA[usability]]></category>

		<guid isPermaLink="false">http://www.haojii.com/?p=148</guid>
		<description><![CDATA[无图无真相，点击下图可以去测试页面查看源码 Gravatar头像这玩意基本上凡事用wordpress的博客的用户都用笔墨介绍过，所以我就不废话了，不知道的自己google一下。 集成Gravatar头像的核心是构造一个图像的URL地址，Gravatar的最简单的头像URL地址是这样生成的 1. 用MD5Hash算法加密你的邮箱地址 2. 头像 URL= http://www.gravatar.com/avatar/+第一步加密后的值 当然Gravatar还提供了几个有意思的参数，如?d=monsterid加上这个参数，如果邮箱地址未在Gravatar注册过，则动态生成一个小怪兽的头像（例子：左图的右下头像） JavaScript的MD5Hash所幸也有人早就写过了，到这里下载一个。 现在我们需要关心的就是页面怎么写了，左图的评论表单是从wordpress里抠出来的，CSS我不是高手，简单的弄了下不至于太难看。 AJAX的标题有点噱头啦，但是在这个例子的基础上做成AJAX评论也只剩下一小步了。 其实测试页面值得一提的代码是这一段，用户输入完email地址之后，这个输入框失去焦点，这个时候我们动态加载其头像，这样一来用户体验能够得到明显提升，不要看是个小小的功能，有时候就是这些细节才抓住了你的目标客户。]]></description>
			<content:encoded><![CDATA[<p>无图无真相，<strong>点击下图可以去测试页面查看源码</strong></p>
<p><a href="http://www.haojii.com/webdev/gravatar/gravatar.html"><img class="size-medium wp-image-147 alignleft" title="gravatar_demo" src="http://www.haojii.com/wp-content/uploads/gravatar_demo-289x300.png" alt="" width="289" height="300" /></a> Gravatar头像这玩意基本上凡事用wordpress的博客的用户都用笔墨介绍过，所以我就不废话了，不知道的自己google一下。</p>
<p>集成Gravatar头像的核心是构造一个图像的URL地址，Gravatar的最简单的头像URL地址是这样生成的</p>
<p>1. 用MD5Hash算法加密你的邮箱地址</p>
<p>2. 头像 URL= http://www.gravatar.com/avatar/+第一步加密后的值</p>
<p>当然Gravatar还提供了几个<a href="http://en.gravatar.com/site/implement/url" target="_blank">有意思的参数</a>，如<strong>?d=monsterid</strong>加上这个参数，如果邮箱地址未在Gravatar注册过，则动态生成一个小怪兽的头像（例子：左图的右下头像）</p>
<p>JavaScript的MD5Hash所幸也有人早就写过了，到<a href="http://pajhome.org.uk/crypt/md5/" target="_blank">这里</a>下载一个。</p>
<p>现在我们需要关心的就是页面怎么写了，左图的评论表单是从wordpress里抠出来的，CSS我不是高手，简单的弄了下不至于太难看。</p>
<p>AJAX的标题有点噱头啦，但是在这个例子的基础上做成AJAX评论也只剩下一小步了。</p>
<p>其实测试页面值得一提的代码是这一段，用户输入完email地址之后，这个输入框失去焦点，这个时候我们动态加载其头像，这样一来用户体验能够得到明显提升，不要看是个小小的功能，有时候就是这些细节才抓住了你的目标客户。</p>
<pre class="brush: jscript; title: ; notranslate">
//jQuery的blur事件
(&quot;#email&quot;).blur(function(event){
//清空上一个头像
$(&quot;#ajaxgravatar&quot;).html('');
$email = $.trim($(&quot;#email&quot;).val()).toLowerCase();
//调用MD5 JavaScript文件的API
$gavatar = hex_md5($email);
//构造URL
$gavatar_url = &quot;http://www.gravatar.com/avatar/&quot;+$gavatar;
//动态修改头像
$(&quot;#ajaxgravatar&quot;).html('&lt;img id=&quot;ajaxgravatar&quot; alt=&quot;Gravatar&quot; src=&quot;'+ $gavatar_url +'?d=monsterid&quot;/&gt;')
});
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.haojii.com/2010/01/integrate-gravatar-for-comment-using-ajax/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

