200多个js技巧代码(4)
生活随笔
收集整理的這篇文章主要介紹了
200多个js技巧代码(4)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
106.動態改變類型 <style> .Overnone?{?border-width:0;background-color:darkblue;cursor:default;color:gold;width:115} .Outnone???{border-width:0;background-color:white;cursor:default;width:115} </style> <input?class=Outnone?onmouseover=this.className='Overnone'?>//107.頁面翻轉 <html?dir=rtl></html>//108.滾屏 parent.scroll(x,y);// <body?onload="s=0"?onDblClick="s=setInterval('scrollBy(0,?1)',10)"?onClick="clearInterval(s)">//109.改變狀態欄 self.status?="";//110.改變窗口大小 window.resizeTo(200,300);//111.改變鼠標樣式 style BODY{CURSOR:?url('mouse.ani'); SCROLLBAR-BASE-COLOR:?#506AA8; SCROLLBAR-ARROW-COLOR:?#14213F; }//112.背景透明 <input?type="button"?value="Button"?style="background-color:?transparent;?border:?0;">//113.鼠標為等待形狀 <input?type=button?onclick="this.style.cursor='wait'">//114.調用父窗口的函數 opener.fucntion1();//115.body的內部html代碼 <input?type="button"?onclick="alert(code.document.body.innerHTML)"?value="查看">//116.框架中調用父窗口的函數 <INPUT?TYPE='button'?onclick='parent.test();'?value='調用parent窗口的函數'>//117.交換節點 <table??width=200??height=200??border> <tr><td??id=c1>CELL_1</td></tr> <tr><td??id=c2>CELL_2</td></tr> </table> <br> <input??type="button"??value="swap??row"??onclick="c1.swapNode(c2)">//118.刪除節點 <table??width=200??height=200??border> <tr?id=trall><td??id=c1>CELL_1</td></tr> <tr><td??id=c2>CELL_2</td></tr> </table> <br> <input??type="button"??value="swap??row"??onclick="trall.removeNode(c2)">//119.添加節點 addNode()//120.獲得事件的父與子標簽 event.srcElement.children[0]和event.srcElement.parentElement?//121.集中為按鈕改變顏色 <style> button{benc:expression(this.onfocus?=?function(){this.style.backgroundColor='#E5F0FF';})} </style> <button>New</button>//122.判斷是左鍵還是右鍵被按下 <body?onmousedown=if(event.button==1)alert("左鍵");if(event.button==2)alert("右鍵")>//123.獲得操作系統的名稱和瀏覽器的名稱 document.write(navigator.userAgent)//124.alt/ctrl/shift鍵按下 event.altKey?//按下alt鍵 event.ctrlKey?//按下ctrl鍵 event.shiftKey?//按下shift鍵125.將當前位置定位為C盤。 {window.location="c:"}//126.返回輸入框的類型 <script> alert(event.srcElement.type);// </script>127.模擬控件的單擊事件 <INPUT?TYPE="hidden"?name="guoguo"?onclick="haha()"> <SCRIPT?LANGUAGE="JavaScript"> <!--function?haha() {alert(); } guoguo.click(); //--> </SCRIPT>//128.取出記錄集的列名 java.sql.ResultSet?rset?=?com.bsitc.util.DBAssist.getIT().executeQuery(queryStatement,?conn); java.sql.ResultSetMetaData?metaData?=?rset.getMetaData(); int?count?=?metaData.getColumnCount(); String?name?=?metaData.getColumnName(i); String?value?=?rset.getString(i);//129.格式化數字 function?format_number(str,digit) {if(isNaN(str)){alert("您傳入的值不是數字!");return?0;}else?if(Math.round(digit)!=digit){alert("您輸入的小數位數不是整數!");return?0;}elsereturn?Math.round(parseFloat(str)*Math.pow(10,digit))/Math.pow(10,digit); }130.回車按鈕轉化為tab按鈕 if(event.keyCode==13)?event.keyCode=9;?//將131.滾動條滾動 <button?onclick="text1.scrollTop=text1.scrollHeight">Scroll</button><br> <textarea?id="text1"?cols=50?rows=10> 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 </textarea>//132.判斷是什么對象 if(typeof(unknown)=="function")return?true; if(typeof(unknown)!="object")return?false;//133.取消文本框自動完成功能 <input?type="text"?autocomplete="off">?//134.讓下拉框自動下拉 <select?onmouseover="javascript:this.size=this.length"?onmouseout="javascript:this.size=1"> <option?value="">1</option> <option?value="">2</option> <option?value="">3</option> </select>?//135.讀取XML文件 var?childrenobj=myselect//document.all.myselect;var?oXMLDoc?=?new?ActiveXObject('MSXML');oXMLDoc.url?=?"mymsg.xml";var?oRoot=oXMLDoc.root;if(oRoot.children?!=?null){for(var?i=0;i<oRoot.children.item(0).children.length;++i){oItem?=?oRoot.children.item(0).children.item(i);oOption?=?new?Option(oItem.text,oItem.value);childrenobj.add(oOption);}} //mymsg.xml文件 <?xml?version="1.0"?encoding="gb2312"??> <childrenlist> <aa> <child?value='3301'>杭州地區</child><child?value='3303'>溫州地區</child></aa> <aa> <child?value='3310'>臺州地區</child><child?value='3311'>麗水地區</child> </aa> </childrenlist>//136.點擊圖片,圖片停止 <a?href="javascript:"><img?src="http://www.51js.com/images/51js/red_forum.gif"?border="0"></a>//137.顯示本地計算機信息 var?WshNetwork?=?new?ActiveXObject("WScript.Network"); alert("Domain?=?"?+?WshNetwork.UserDomain); alert("Computer?Name?=?"?+?WshNetwork.ComputerName); alert("User?Name?=?"?+?WshNetwork.UserName);//138.比較時間tDate?=?new?Date(2004,01,08,14,35);?//年,月,日,時,分dDate?=?new?Date();tDate<dDate?alert("早于"):alert("晚于");//139.彈出鼠標所在處的鏈結地址<body?onmouseover="if?(event.srcElement.tagName=='A')alert(event.srcElement.href)"><ahref="http://51js.com/viewthread.php?tid=13589"?>dddd</a><input>//140.注意不能通過與?undefined?做比較來測試一個變量是否存在,雖然可以檢查它的類型是否為“undefined”。在以下的代碼范例中,假設程序員想測試是否已經聲明變量?x?: //?這種方法不起作用 if?(x?==?undefined)//?作某些操作 //?這個方法同樣不起作用-?必須檢查//?字符串?"undefined" if?(typeof(x)?==?undefined)//?作某些操作 //?這個方法有效 if?(typeof(x)?==?"undefined")//?作某些操作141.創建具有某些屬性的對象 var?myObject?=?new?Object(); myObject.name?=?"James"; myObject.age?=?"22"; myObject.phone?=?"555?1234";//142.枚舉(循環)對象的所有屬性 for?(var?a?in?myObject) {//?顯示?"The?property?'name'?is?James",等等。window.alert("The?property?'"?+?a?+?"'?is?"?+?myObject[a]); }//143.判斷一個數字是否是整數 var?a=23.2; alert(a%1==1)//144.新建日期型變量 var?a?=?new?Date(2000,?1,?1); alert(a.toLocaleDateString());145.給類定義新的方法 function?trim_1() {return?this.replace(/(^\s*)|(\s*$)/g,?""); } String.prototype.trim=trim_1; alert('cindy'.trim());146.定義一個將日期類型轉化為字符串的方法 function?guoguo_date() {var?tmp1,tmp2;tmp1?=this.getMonth()+1+"";if(tmp1.length<2)tmp1="0"+tmp1;tmp2?=this.getDate()+"";if(tmp2.length<2)tmp2="0"+tmp2;return?this.getYear()+"-"+tmp1+"-"+tmp2; } Date.prototype.toLiteString=guoguo_date; alert(new?Date().toLiteString())147.?pasta?是有四個參數的構造器,定義對象。 function?pasta(grain,?width,?shape,?hasEgg) {//?是用什么糧食做的?this.grain?=?grain;//?多寬?(數值)this.width?=?width;????//?橫截面形狀?(字符串)this.shape?=?shape;??//?是否加蛋黃?(boolean)this.hasEgg?=?hasEgg;?//定義方法this.toString=aa; } function?aa() {; } //定義了對象構造器后,用?new?運算符創建對象實例。 var?spaghetti?=?new?pasta("wheat",?0.2,?"circle",?true); var?linguine?=?new?pasta("wheat",?0.3,?"oval",?true); //補充定義屬性,spaghetti和linguine都將自動獲得新的屬性 pasta.prototype.foodgroup?=?"carbohydrates";148.打印出錯誤原因 try {x?=?y???//?產生錯誤。 } catch(e) {document.write(e.description)???//打印?"'y'?is?undefined". }//149.生成EXCEL文件并保存 var?ExcelSheet; ExcelApp?=?new?ActiveXObject("Excel.Application"); ExcelSheet?=?new?ActiveXObject("Excel.Sheet"); //本代碼啟動創建對象的應用程序(在這種情況下,Microsoft?Excel?工作表)。一旦對象被創建,就可以用定義的對象變量在代碼中引用它。?在下面的例子中,通過對象變量?ExcelSheet?訪問新對象的屬性和方法和其他?Excel?對象,包括?Application?對象和?ActiveSheet.Cells?集合。 //?使?Excel?通過?Application?對象可見。 ExcelSheet.Application.Visible?=?true; //?將一些文本放置到表格的第一格中。 ExcelSheet.ActiveSheet.Cells(1,1).Value?=?"This?is?column?A,?row?1"; //?保存表格。 ExcelSheet.SaveAs("C:\\TEST.XLS"); //?用?Application?對象用?Quit?方法關閉?Excel。 ExcelSheet.Application.Quit();//150.根據標簽獲得一組對象 var?coll?=?document.all.tags("DIV"); if?(coll!=null) { for?(i=0;?i<coll.length;?i++) ... }//轉載于:https://my.oschina.net/u/1014520/blog/279640
總結
以上是生活随笔為你收集整理的200多个js技巧代码(4)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 重温WCF之会话Session(九)
- 下一篇: 微软代码签名证书使用指南