天天看點

常用的一些javascript小技巧

常用的一些javascript小技巧

[作者:oror,from藍色理想]

事件源對象 

event.srcElement.tagName 

event.srcElement.type 

………………………………

<a></a>

捕獲釋放 

event.srcElement.setCapture();  

event.srcElement.releaseCapture();

事件按鍵

event.keyCode 

event.shiftKey 

event.altKey 

event.ctrlKey 

事件傳回值

event.returnValue 

滑鼠位置 

event.x 

event.y 

窗體活動元素

document.activeElement 

綁定事件

document.captureEvents(Event.KEYDOWN); 

通路窗體元素 

document.all("txt").focus(); 

document.all("txt").select(); 

窗體指令 

document.execCommand 

窗體COOKIE 

document.cookie 

菜單事件 

document.oncontextmenu

建立元素

document.createElement("SPAN"); 

根據滑鼠獲得元素:

document.elementFromPoint(event.x,event.y).tagName=="TD 

document.elementFromPoint(event.x,event.y).appendChild(ms)

窗體圖檔

document.images[索引] 

窗體事件綁定 

document.onmousedown=scrollwindow; 

元素 

document.窗體.elements[索引]

對象綁定事件 

插件數目

navigator.plugins 

取變量類型

typeof($js_libpath) == "undefined" 

下拉框

下拉框.options[索引] 

下拉框.options.length 

查找對象 

document.getElementsByName("r1"); 

document.getElementById(id); 

定時 

UNCODE編碼 

escape() ,unescape 

父對象 

obj.parentElement(dhtml) 

obj.parentNode(dom)

交換表的行

TableID.moveRow(2,1) 

替換CSS 

document.all.csss.href = "a.css"; 

并排顯示 

display:inline

隐藏焦點 

hidefocus=true 

根據寬度換行

style="word-break:break-all"

自動重新整理 

&lt;meta HTTP-EQUIV="refresh" CONTENT="8;URL=http://c98.yeah.net"&gt;

簡單郵件

&lt;a href="mailto:[email protected]?subject=ccc&amp;body=xxxyyy"&gt; 

快速轉到位置 

obj.scrollIntoView(true)

&lt;a name="first"&gt; 

&lt;a href="#first"&gt;anchors&lt;/a&gt; 

網頁傳遞參數

location.search();

可編輯

obj.contenteditable=true 

執行菜單指令

obj.execCommand 

雙位元組字元 

/[^\x00-\xff]/ 

漢字 

/[\u4e00-\u9fa5]/ 

讓英文字元串超出表格寬度自動換行

word-wrap: break-word; word-break: break-all; 

透明背景

&lt;IFRAME src="1.htm" width=300 height=180 allowtransparency&gt;&lt;/iframe&gt; 

獲得style内容

obj.style.cssText 

HTML标簽

document.documentElement.innerHTML 

第一個style标簽

document.styleSheets[0] 

style标簽裡的第一個樣式

document.styleSheets[0].rules[0] 

防止點選空連結時,頁面往往重置到頁首端。

&lt;a href="javascript:function()"&gt;word&lt;/a&gt; 

上一網頁源

asp: 

request.servervariables("HTTP_REFERER") 

javascript: 

document.referrer 

釋放記憶體

CollectGarbage();

禁止右鍵 

document.oncontextmenu = function() { return false;} 

禁止儲存 

&lt;noscript&gt;&lt;iframe src="*.htm"&gt;&lt;/iframe&gt;&lt;/noscript&gt;

禁止選取

&lt;body oncontextmenu="return false" ondragstart="return false" onselectstart ="return false" 

onselect="document.selection.empty)" oncopy="document.selection.empty)" onbeforecopy=

"return false"onmouseup="document.selection.empty()&gt; 

禁止粘貼

&lt;input type=text onpaste="return false"&gt; 

位址欄圖示 

&lt;link rel="Shortcut Icon" href="favicon.ico"&gt;

favicon.ico 名字最好不變16*16的16色,放虛拟目錄根目錄下 

收藏欄圖示 

&lt;link rel="Bookmark" href="favicon.ico"&gt; 

檢視源碼

關閉輸入法

&lt;input style="ime-mode:disabled"&gt; 

自動全選

&lt;input type=text name=text1 value="123" onfocus="this.select()"&gt; 

ENTER鍵可以讓光标移到下一個輸入框

&lt;input onkeydown="if(event.keyCode==13)event.keyCode=9"&gt;

文本框的預設值

&lt;input type=text value="123" onfocus="alert(this.defaultValue)"&gt; 

title換行

obj.title = "123 sdfs " 

獲得時間所代表的微秒 

var n1 = new Date("2004-10-10".replace(/-/g, "\/")).getTime() 

視窗是否關閉 

win.closed 

checkbox扁平

&lt;input type=checkbox style="position: absolute; clip:rect(5px 15px 15px 5px)"&gt; 

擷取選中内容 

document.selection.createRange().duplicate().text

自動完成功能

&lt;input  type=text  autocomplete=on&gt;打開該功能  

&lt;input  type=text  autocomplete=off&gt;關閉該功能 

視窗最大化 

&lt;body onload="window.resizeTo(window.screen.width - 4,window.screen.height-50);window.moveTo(-4,-4)"&gt; 

無關閉按鈕IE

window.open("aa.htm", "meizz", "fullscreen=7"); 

統一編碼/解碼

alert(decodeURIComponent(encodeURIComponent("http://你好.com?as= hehe"))) 

encodeURIComponent對":"、"/"、";" 和 "?"也編碼 

表格行訓示

//各種尺寸 

s  +=  "\r\n網頁可見區域寬:"+  document.body.clientWidth;   

s  +=  "\r\n網頁可見區域高:"+  document.body.clientHeight;   

s  +=  "\r\n網頁可見區域高:"+  document.body.offsetWeight  +"  (包括邊線的寬)";   

s  +=  "\r\n網頁可見區域高:"+  document.body.offsetHeight  +"  (包括邊線的寬)";   

s  +=  "\r\n網頁正文全文寬:"+  document.body.scrollWidth;   

s  +=  "\r\n網頁正文全文高:"+  document.body.scrollHeight;   

s  +=  "\r\n網頁被卷去的高:"+  document.body.scrollTop;   

s  +=  "\r\n網頁被卷去的左:"+  document.body.scrollLeft;   

s  +=  "\r\n網頁正文部分上:"+  window.screenTop;   

s  +=  "\r\n網頁正文部分左:"+  window.screenLeft;   

s  +=  "\r\n螢幕分辨率的高:"+  window.screen.height;   

s  +=  "\r\n螢幕分辨率的寬:"+  window.screen.width;   

s  +=  "\r\n螢幕可用工作區高度:"+  window.screen.availHeight;   

s  +=  "\r\n螢幕可用工作區寬度:"+  window.screen.availWidth; 

//不緩存 

&lt;META HTTP-EQUIV="pragma" CONTENT="no-cache"&gt; 

&lt;META HTTP-EQUIV="Cache-Control" CONTENT="no-cache, must-revalidate"&gt; 

&lt;META HTTP-EQUIV="expires" CONTENT="0"&gt; 

//正則比對 

比對中文字元的正規表達式: [\u4e00-\u9fa5] 

比對雙位元組字元(包括漢字在内):[^\x00-\xff] 

比對空行的正規表達式:\n[\s| ]*\r 

比對HTML标記的正規表達式:/&lt;(.*)&gt;.*&lt;\/\1&gt;|&lt;(.*) \/&gt;/  

比對首尾空格的正規表達式:(^\s*)|(\s*$)(像vbscript那樣的trim函數) 

比對Email位址的正規表達式:\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)* 

比對網址URL的正規表達式:http://([\w-]+\.)+[\w-]+(/[\w- ./?%&amp;=]*)? 

以下是例子: 

利用正規表達式限制網頁表單裡的文本框輸入内容:

//消除圖像工具欄

[/html]

&lt;IMG SRC="mypicture.jpg" HEIGHT="100px" WIDTH="100px" GALLERYIMG="false"&gt;  

or 

&lt;head&gt; 

&lt;meta http-equiv="imagetoolbar" content="no"&gt; 

&lt;/head&gt; 

//取得控件得絕對位置(1)

{

runEx('temp91492')

}" type=button&gt; [Ctrl+A 全部選擇 提示:你可先修改部分代碼,再按運作] 

//獲得控件的絕對位置(2) 

oRect = obj.getBoundingClientRect(); 

oRect.left 

oRect.

//列印分頁

&lt;p  style="page-break-after:always"&gt;page1&lt;/p&gt;   

&lt;p  style="page-break-after:always"&gt;page2&lt;/p&gt;   

//設定列印

runEx('temp1901')

//自帶的列印預覽 

WebBrowser.ExecWB(1,1) 打開  

Web.ExecWB(2,1) 關閉現在所有的IE視窗,并打開一個新視窗  

Web.ExecWB(4,1) 儲存網頁  

Web.ExecWB(6,1) 列印  

Web.ExecWB(7,1) 列印預覽  

Web.ExecWB(8,1) 列印頁面設定  

Web.ExecWB(10,1) 檢視頁面屬性  

Web.ExecWB(15,1) 好像是撤銷,有待确認  

Web.ExecWB(17,1) 全選  

Web.ExecWB(22,1) 重新整理  

Web.ExecWB(45,1) 關閉窗體無提示  

&lt;style media=print&gt;  

.Noprint{display:none;}&lt;!--用本樣式在列印時隐藏非列印項目--&gt;  

.PageNext{page-break-after: always;}&lt;!--控制分頁--&gt;  

&lt;/style&gt;  

&lt;object  id="WebBrowser"  width=0  height=0  classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"&gt;     

&lt;/object&gt;     

&lt;center class="Noprint" &gt; 

&lt;input type=button value=列印 onclick=document.all.WebBrowser.ExecWB(6,1)&gt;  

&lt;input type=button value=直接列印 onclick=document.all.WebBrowser.ExecWB(6,6)&gt;  

&lt;input type=button value=頁面設定 onclick=document.all.WebBrowser.ExecWB(8,1)&gt;  

&lt;/p&gt;  

&lt;p&gt; &lt;input type=button value=列印預覽 onclick=document.all.WebBrowser.ExecWB(7,1)&gt;  

&lt;/center&gt;

//去掉列印時的頁眉頁腳 

runEx('temp79875')

//無模式的提示框 

function modelessAlert(Msg) 

   window.showModelessDialog("javascript:alert(\""+escape(Msg)+"\");window.close();","","status:no;resizable:no;help:no;dialogHeight:height:30px;dialogHeight:40px;"); 

//下載下傳檔案 

function DownURL(strRemoteURL,strLocalURL) 

 try 

 { 

  var xmlHTTP=new ActiveXObject("Microsoft.XMLHTTP"); 

  xmlHTTP.open("Get",strRemoteURL,false); 

  xmlHTTP.send(); 

  var adodbStream=new ActiveXObject("ADODB.Stream"); 

  adodbStream.Type=1;//1=adTypeBinary 

  adodbStream.Open(); 

  adodbStream.write(xmlHTTP.responseBody); 

  adodbStream.SaveToFile(strLocalURL,2); 

  adodbStream.Close(); 

  adodbStream=null; 

  xmlHTTP=null; 

 } 

 catch(e) 

  window.confirm("下載下傳URL出錯!"); 

 //window.confirm("下載下傳完成."); 

//檢驗連接配接是否有效 

function getXML(URL)  

 var xmlhttp = new ActiveXObject("microsoft.xmlhttp"); 

 xmlhttp.Open("GET",URL, false);  

 {  

  xmlhttp.Send(); 

 catch(e){} 

 finally  

  var result = xmlhttp.responseText; 

  if(result)  

  { 

   if(xmlhttp.Status==200) 

   { 

    return(true); 

   } 

   else  

    return(false); 

  } 

  else  

   return(false); 

}

//檢查網頁是否存在 

function CheckURL(URL) 

  var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 

  xmlhttp.Open("GET",URL, false); 

  try 

  {  

    xmlhttp.Send();  

    var result = xmlhttp.status; 

  catch(e) {return(false); } 

  if(result==200) 

    return true; 

  xmlhttp = null; 

  return false; 

//禁止FSO 

1.登出元件 

regsvr32 /u scrrun.dll 

2.修改PROGID 

HKEY_CLASSES_ROOT\Scripting.FileSystemObject 

Scripting.FileSystemObject 

3.對于使用object的使用者,修改HKEY_CLASSES_ROOT\Scripting. 

本文轉自快樂就好部落格園部落格,原文連結:http://www.cnblogs.com/happyday56/archive/2007/11/01/946134.html,如需轉載請自行聯系原作者

繼續閱讀