發表文章

目前顯示的是有「Javascript」標籤的文章

javascript 基本語法

嵌入外部檔案 方式一:在<head>標韱加入<script src="js檔名" ></script> 方式二:透過程式化的方式動態將其載入 <script>         var head = document.getElementsByTagName('HEAD')[0];         var sc = document.createElement('script');         sc.src = 'js檔名';         head.appendChild(sc); </script>

javascript找對象與使用對象方法

<!DOCTYPE html> <html lang="en"> <head>     <meta charset="UTF-8">     <meta name="viewport" content="width=device-width, initial-scale=1.0">     <meta http-equiv="X-UA-Compatible" content="ie=edge">     <title>找對象</title> </head> <body>     <h4>標題</h4>     <div id="test1">         <p>1</p>         <p>2</p>         <p>3</p>     </div>     <div class="test2" >         test     </div>     username:<input type="text" name="username" value="Mark">     username:<input type="text" name="username2" value="Mark"> <script>  //『用ID查詢』  //返回對象 ( objectHTMLElement ) console.log(document.getElementById("test1"));  //操作方式 ...

常用DOM物件

    注 :window 對象是瀏器器宿主對象,和JS語言無關     window 對象的方法:     window.alert(message)     window.confirm(message)     window.prompt(message,default)     window.colse()關閉窗口     window.print()打印     window.setlntval(表達式,毫秒)//按照指定的周期(以毫秒計)來調用函數或計算表達式,調整可以執行一次。     window.clearintval(定時器對象)//取消由setInterval() 設置的timeout。     window.setTimeout(表達式,毫秒)//在指定的毫秒數後調用函數或計算表達式,調整只執行code一次。     window.clearTimeout(定時器對象)//取消由setTimeout() 方法設置的timeout。     window對象的子對象:     1、navigator 瀏覽器信息對象:     appCodeName 內部代碼     appName 瀏覽器名稱     appVersion 瀏覽器版本     platform 操作系統類型     userAgent 用戶代理訊息     cookieEnabled 是否支持cookie     2、location 地址欄對象:     host主機     port端口     href地址     alhname路徑     protocol協議     search 查詢字符串 ...

javascript 常用函數

    /*     //****************     //string 字符串對象     //****************     length屬性:長度     concat(String)連線兩個或更多個字符串     indexOf(string)返回出現字符串的位置     substr(num1,num2)截取字符串     toLoweCase()轉換成小寫     toUpperCase()轉換成大寫     replace(str1,str2)字符串替換    */    var str1 = 'abcdefg';    var str2 = 'YZF';    console.log(str1.length); //7    console.log(str1.concat(str2)); //abcdefgYZF    console.log(str1.indexOf('a')>=0 ? "true" : "false");//true    console.log(str1.substr(2,5));//cdefg    console.log(str2.toLowerCase());//yzf    console.log(str1.toUpperCase());//ABCDEFG    console.log(str1.replace('c','zzz'));//abzzzdefg    /*     //****************     //Date日期對象     //****************     getFullYear()返回年份(...

JSLint的錯誤提示都是什麼意思

前端javascript代碼編寫中,有一個不錯的工具叫JSLint,可以檢查代碼規範化,壓縮JS,CSS等,但是他的語法規範檢查個人覺得太“苛刻”了,會提示各種各樣的問題修改建議,有時候提示的信息我們看的莫名其妙,這裡,先轉載一下攜程UED的一個技術文章(原文鏈接http://ued.ctrip.com/blog/?p=2733),看看JSLint的錯誤提示都是什麼意思: “Missing semicolon.” : “缺少分號.”, “Use the function form of \”use strict\”.” : “使用標準化定義function.”, “Unexpected space after '-'.” : “在'- '後面不應出現空格.”, “Expected a JSON value.” : “請傳入一個json的值.”, “Mixed spaces and tabs.”: “空格和TAB重複.”, “Unsafe character.” : “不安全的字符.”, “Line too long.”: “本行中的字符超過設定的最大長度.”, “Trailing whitespace.”: “本行末尾有過多無用空格.”, “Script URL.” : “腳本URL.”, “Unexpected {a} in '{b}'.” : “在'{b}'中不該出現{a}.”, “Unexpected '{a}'.” : “不該在此出現'{a}'.”, “Strings must use doublequote.” : “字符串需要用雙引號”, “Unnecessary escapement.” : “不需要轉義”, “Control character in string : {a}.” : “在字符串中出現了Control的字符”, “Avoid \\' .” : “避免\\”, “Avoid \\v .” : “避免\\v ”, “Avoid \\x -.” : “避免\\x -”, “Bad escapement.” : “錯誤的轉義字符”, “Bad number '{a}'.” : “錯...
Q1:在 JavaScript 中 undefined 、 null 及 空字串 的定義 Q2:輸出產生亂碼 Q1:在 JavaScript 中 undefined 、 null 及 空字串 的定義     /*         一、null 表示沒有值,被視為一個物件;         二、有以下情形會被視為undefined:             (1). 該變數未被宣告             (2). 被宣告的變數未給值。         三、 "" 表示空字串,被視為一個字串,與 null 不同     */     var nullObj = null;     var undifindedObj;     document.write("null是一個" + typeof (null) + "<br>");     document.write("undifindedObj變數未給值,故是一個" + typeof (undifindedObj) + "<br>");     document.write("undifindedVar變數未被宣告,故是一個" + typeof (undifindedVar) + "<br>");     document.write("空字串是一個" + typeof ("") + "<br>");  /* 執行結果: null是一個object undifindedObj變數未給值,故是一個undefined undifindedVar變數未被宣告,故是一個undefined 空字串是一個string  */ Q2:輸出產生亂碼 如果 Web application 的編碼規則是 utf...