2015年2月15日 星期日

javascript 隨手記

1:
browser的寬高
document.body.clientHeight
document.body.clientWidth

2:
 <body onload="resizeImg();" onresize="resizeImg();">

3:navigator.cookieEnable.  查詢cookie

4: window.onload = function(){}; 直接在script使用.不用在<body>

5:
<input id="zipcode" name="zipcode" type="text"  onclick="showIt(this.form)" />
function showit(theform)
{
   alert(theform["zipcode"].value);
}

6:onfocus , onblur , onchange.
onblur跟onchange差別為在切換不同元件即觸發. onchange必須加上數值更動的條件

7:isNanN(inputField.value)

8:
function place(form)
{
    form.submit();
}

<intput type="button" value="haha" onlick="place(this.form)">

9:正規表示
. 匹配所有字符,換行符號除外
\d 所有數字
\w 所有數字字母
\s 空格
^  模式開始
$  模式結束
/^\d/   字串開始需要有1各數字
/cat$/   cat作為結尾
/^cat/   cat作為開頭
*    限制符前的子模式0或多次
+                               1或多次
?                               0或1次
{n}       {min,max}                     需出現n次
() 集合字符或/和原字符 , 成為子模式.

var rule=/^\d{5}$/;
if(!rule.test(inputField.value))

/(red | blue) pill/

 10:
/d[iu]g/      "dig" "dug"  是ok的
/\$\d[\d\.]*/    "$3.50"   "$19.95" 是ok的

11: email 驗證
/^[\w\.-_\+]+@[\w-]+(\.\w{2,3})+$/

12:
getElementByID
getElementByTagName("div")
getElementByTagName("div")[3]

13:
nodeValue nodeType  childNodes    firstChild     lastChild
document.getElementById("haha").nodeValue
document.getElementByTagName("body")[0].childNodes[1].lastChild

var node=document.getElementById("haha");
while(node.firstChild)
    node.removeChild(node.firstChild);
node.appendChild(document.createTextNode("Ok,maybe you are alond."));

 14:
span div 差異. 都是作為段落的區隔 , 但是div 有換行. div類似大括弧 , span類似大括弧內的小括弧.

15:
<span id="haha1" class="haha">haha</span>
document.getElementById("haha").className

16:
onmouseover: 進入button,
onmouseout: 離開button.

17:
document.getElementById("haha").style.visibility = "visible"; //visible or hidden

18:
<form action=/haha/haha method=POST name="setconfig">

 <input type="hidden" name="session_key" id="IDSessionKey" value="123456">
  <script>
  var sessionKey = <% getCurSessionKey();%>;
  document.wlanSetup.elements["session_key"].value=sessionKey;
  </script>

 </form>

19: 二維陣列
var seats = new Array(new Array(9),new Array(9),new Array(9));

20.
<input type="button" value="Order Banner" onclick="placeOrder(this.form);" />

function placeOrder(form) {
        if (validateNonEmpty(form["message"], form["message_help"]) && <---對應form的name
}

21:事件
onforcus , onblur/onchange(移至下一個目標 ,差異為onchange內容被改時觸發)

22:動態修改網頁
var history = document.getElementById("history");
        if (curScene != 0) {
          // Add the latest decision to the history
          var decisionElem = document.createElement("p");
          decisionElem.appendChild(document.createTextNode("Decision " + decision + " -> Scene " + curScene + " : " + message));
          history.appendChild(decisionElem);
        }

23:
var now = new Date();
var between = (time2 - time1)/1000*60*60*24
return Math.round(between) ; #四捨五入

24:
new Blog("Got the new cube I ordered. It's a real pearl.", new Date("08/14/2008")),

function Blog(body, date) {
        // Assign the properties
        this.body = body;
        this.date = date;
      }

25: 函數自定義
var num = [2,4,1];
function compare(){
    return x-y;
}

num.sort(compare);

blog.sort(function(blog1,blog2){return blog2.date-blog1.date;});

26: indexof 找字串 , 找不到則 -1 , return 位置

27:
blog.sort(function(blog1,blog2){return blog2.date-blog1.date;});
round(); //四捨五入成整數
floor(); //浮點數捨去
ceil();//浮點數無條件進位整數
random();//產生0~1隨機數

產生隨機0~5
var num = Math.floor(Math.random()*6);

28
Blog.prototype.toHTML = function(){};
prototype在new class的時候. toHTML只會有一份,並不會因為new多個就有多份.

29:
Blog.prototype.signature = "by Puzzler Ruby";

30:
util.inherits node.js 繼承時使用
Base.prototype.showName   才會被繼承.

31:
util.inspect   debug用

32:事件調用
emitter.on  emitter.emit

33:
function say_haha(line){
    print(this.name,"say haha",line);
}
var haha={name:"jimmy" , speak:say_haha};
haha.speak("oh ~~~ ya");
或者
say_haha.apply(haha,["oh~~~ya"]);
say_haha.call(haha,"oh~~~ya");

沒有留言:

張貼留言