JavaScript Remove Element From Array

  1. Array.prototype.removeByElement = function(element) {
  2.     for(var i=0; i<this.length; i++) {
  3.         if (this[i] == element) {
  4.             this.splice(i, 1);    // (start position, select length)
  5.         }
  6.     }
  7.     return this;
  8. }

Tags: , ,

星期一, 三月 8th, 2010 JavaScript, 程式片段, 程式筆記 無迴響

JavaScript 的簡短寫法

在網上遊網時發現原來 JavaScript 是屬於函式語言?所以在 if, else 中失去了效率,變相就有了以下的取代方法,
整理了一下代碼如下:

  1. // 先定義要用到的變量
  2. var a = 1, b = 0 , c = "";
  3.  
  4. /* 寫法一 */
  5. // 簡寫:
  6. a && (c += "OK");
  7.  
  8. // 正寫:
  9. if (a) {
  10.     c += "OK";
  11. }
  12.  
  13. /* 寫法二 */
  14. // 簡寫:
  15. b || (b = 2);
  16.  
  17. // 正寫:
  18. if (!b) {
  19.     b = 2;
  20. }
  21.  
  22. /* 寫法三 */
  23. // 簡寫:
  24. a ? ( (c = "yes"), (b = 1) ) : ( (c = "no"), (b = 2) );
  25.  
  26. // 正寫
  27. if (a) {
  28.     c = "yes";
  29.     b = 1;
  30. }else{
  31.     c = "no";
  32.     b = 2;
  33. }
  34.  
  35. /* 寫法四 */
  36. // 簡寫 (1):
  37. (a == window.getElementById("c")) && (a.style.display = "none");
  38.  
  39. // 簡寫 (2):
  40. (a == window.getElementById("c")) ? a.style.display = "none" : "";
  41.  
  42. // 正寫:
  43. if (a == window.getElementById("c")) {
  44.     a.style.display = "none";
  45. }
  46.  
  47. /* 寫法五 */
  48. // 簡寫
  49. return a.nodeType != 3 
  50.         ? a.tagName 
  51.         : a.setIntval 
  52.             ? "window" 
  53.             : "other"
  54.  
  55. // 正寫
  56. if (a.nodeType != 3) {
  57.     return a.tageName;
  58. }else if (a.setIntval) {
  59.     return "window";
  60. }else{
  61.     return "other";
  62. }

Tags: ,

星期日, 三月 7th, 2010 JavaScript, 程式片段, 程式筆記 無迴響

table auto height 100%

因為在某些情況下要使用到 table,所以花了點昤間來研究.
結果在 IE8, Chrome, FireFox 3.6 都測試成功了,記錄一下

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
  2. <html> 
  3. <head> 
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
  5. <title>x</title>
  6. <style type="text/css">
  7. * { border: none; padding: 0; margin: 0; z-index:0; }
  8. body, html { height: 100%; }
  9. #wapper { height:100%; min-height:100%; width: 100%; background-color:#000; color: #FFF; }
  10. </style>
  11. </head>
  12. <body>
  13. <div id="wapper">
  14.     <table width="100%" height="100%">
  15.     <tr>
  16.         <td>1</td>
  17.         <td>2</td>
  18.     </tr>
  19.     <tr>
  20.         <td>1</td>
  21.         <td>2</td>
  22.     </tr>
  23.     </table>
  24. </div>
  25. </body>
  26. </html>

Tags: , , , ,

星期六, 三月 6th, 2010 html, 程式片段, 程式筆記 迴響已關閉

Python 中計算字串中的字符出現次數

  1. #縮成一行:
  2. s = 'abCdEab@a!{(]zyu&8jb'; print "\n".join(map(lambda x: "%s -> %d" % (x, s.count(x)), s))
  3.  
  4. #分成多行
  5. s = 'abCdEab@a!{(]zyu&8jb'
  6. f = lambda x: "%s -> %d" % (x, s.count(x));
  7. e = map(f, s)
  8. print "\n".join(e)
  9.  
  10. #備用
  11. print [i for i in s]

Tags: , ,

星期二, 三月 2nd, 2010 Python, 程式片段, 程式筆記 迴響已關閉

2009-03-02 網摘

Node.js
SQLite bindings for Node.js
pure Javascript MySQL network driver for node.js
BSlayer (MySQL) support for Node.JS
WebSocket-compatible realtime HTTP server implemented with Node.JS
simple chat demo for node

PHP
PHP jQuery AJAX Javascript Long Polling
php framework / php微型智能框架 / jquery Similar Operation

jQuery
Javascript jQuery plugin for hooking keyboard events

CSS
CSS 3.0 ?考手? (中文版)

IIS
Internet Information Services 6.0 may not function correctly after installing KB973917

Tags: , , , , , ,

星期二, 三月 2nd, 2010 網摘 迴響已關閉

PHP 通過 .htaccess 修改上傳檔案大小

  1. php_value upload_max_filesize 32M
  2. php_value post_max_size 32M
  3. php_value max_execution_time 360
  4. php_value max_input_time 360

Tags: , , , , ,

星期六, 二月 27th, 2010 PHP, 伺服器 迴響已關閉

Python 找出陣列中的相同值

  1. # -*- coding: utf-8 -*-
  2.  
  3. a = ["11","22","33"]
  4. b = ["11","33"]
  5. c = set(a) & set(b)
  6.  
  7. print c #输出 set(['11', '33'])

Tags: , ,

星期六, 二月 27th, 2010 Python, 程式筆記 迴響已關閉

判斷是否為 Window

  1. function isWindowObject(obj){
  2.    return obj.window === obj.window.window
  3. }

參考: http://blog.csdn.net/cheng5128/archive/2010/02/20/5313654.aspx

Tags: , ,

星期日, 二月 21st, 2010 JavaScript, 程式片段, 程式筆記 迴響已關閉

Python 中取得昨天的日期

  1. import datetime
  2. last_date = datetime.date.today() - datetime.timedelta(days=1)
  3. print last_date

Tags: , , ,

星期日, 二月 21st, 2010 Python, 程式片段, 程式筆記 迴響已關閉

PHP5 的測試筆記 (closure, lambda)

  1. class XRuby {
  2.     private $from, $to, $table;
  3.    
  4.     public function __construct($from = "", $to = "", $table = array()) {
  5.         $this->from = $from;
  6.         $this->to = $to;
  7.         $this->table = $table;
  8.     }
  9.  
  10.     public function eachRange($func) {
  11.         if (is_callable($func)) {
  12.  
  13.             $yield = function() use ($func) {
  14.                 $args = func_get_args();
  15.                 call_user_func_array($func, $args);
  16.             };
  17.  
  18.             for($i=$this->from; $i<$this->to; $i++) {
  19.                 $yield($i);   
  20.             }
  21.         }
  22.     }
  23.  
  24.     public function eachArray($func) {
  25.         if (is_callable($func)) {
  26.             foreach($this->table as $k => $v) {
  27.                 call_user_func_array($func, array($k, $v));
  28.             }
  29.         }
  30.     }
  31. }
  32.  
  33. $s = function($num) {
  34.     echo "<strong>Test Case: ".($num)."</strong><br />";
  35. };
  36.  
  37. $x = new XRuby(1, 10, array(1 => 'one', 2 => 'two'));
  38.  
  39. $s(1);
  40. $x->eachRange(function($i) {
  41.     echo ($i*$i)."<br />";
  42. });
  43.  
  44. $s(2);
  45. $x->eachArray(function($k, $v) {
  46.     printf("%s => %s<br />", $k, $v);
  47. });
  48.  
  49. $s(3);
  50. array_walk(
  51.     $t = array('1' => 'one', '2' => 'two', '3' => 'three'),
  52.     function(&$v, $k) {
  53.         printf("%s => %s<br />", $k, $v);
  54.     }
  55. );

Tags: , , ,

星期三, 二月 17th, 2010 PHP, 程式片段, 程式筆記 無迴響