JS實例——w3cschool

基礎實例

  • write()
document.write("this is a string");//生成普通文本
document.write("<br>"+Date());//html+函數
document.write("<input type='text'>");//html,生成標籤
  • JS代碼塊
    JS中沒有塊作用域,只有函數作用域。JS代碼塊只是把需要一起執行的語句進行分組。
  • prompt()
var name = prompt("please input your name:","Anne")//後面的參數爲輸入框的默認值
  • confirm()
var yourChoice = confirm("please confirm your choice!")
//yourChoice == true,選擇了“確認”
//yourChoice == false,選擇了“取消”
  • break
    中斷,switch,if等語句
  • continue
    跳出本次循環,繼續下次循環
  • for in
    遍歷數組內元素
for(index in Array)//index爲數組內元素索引,從0開始
  • onerror事件
    捕獲網頁中的錯誤。(chrome、opera、safari 瀏覽器不支持)
    只要頁面中出現腳本錯誤,就會產生 onerror 事件。如果需要利用 onerror 事件,就必須創建一個處理錯誤的函數。你可以把這個函數叫作 onerror 事件處理器 (onerror event handler)。這個事件處理器使用三個參數來調用:msg(錯誤消息)、url(發生錯誤的頁面的 url)、line(發生錯誤的代碼行)。
<html>
<head>
<script type="text/javascript">
onerror=handleErr
var txt=""

function handleErr(msg,url,l)
{
txt="There was an error on this page.\n\n"
txt+="Error: " + msg + "\n"
txt+="URL: " + url + "\n"
txt+="Line: " + l + "\n\n"
txt+="Click OK to continue.\n\n"
alert(txt)
return true
}

function message()
{
adddlert("Welcome guest!")
}
</script>
</head>

<body>
<input type="button" value="View message" onclick="message()" />
</body>

</html>

高級實例

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章