郭家秀
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # JS 基礎入門 ###### tags:`javascript` ## JS 環境與變數 ### 變數 #### 原始型別 - Number數字 - String字串("" / '') - boolean布林(true / false) - undefined(設了一個變數但沒有賦予值) - null - sybol #### JS 的執行過程 1. 建立出執行環境 會將變數的值暫存至瀏覽器的記憶體 變數會指向某個記憶體位置,其存放的是變數的值 若變數的值有變動,記憶體會再新增一個空間存放新的值 ![](https://i.imgur.com/409ksH0.jpg) Q1:幾個型別、幾個變數、產生過幾個記憶體位置 A:四個型別、一個變數、五個記憶體位置 > undefined也是一種型別,也會佔用一個記憶體位置 ```javascript= var a; //重新賦予新的值,不是修改 a = 1; a = 3; a = 'hello'; a = true; ``` Q2:幾個型別、幾個變數、產生過幾個記憶體位置 A:三個型別、兩個變數、五個記憶體位置 ```javascript= var a = 1; var b = 2; var b = 'qq';//新增一個記憶體空間存放'qq' //創造階段 var a;//會先有undefined的記憶體空間 var b; a=1;//新增一個記憶體空間存放1,a指向1 b=2;//新增一個記憶體空間存放2,b指向2 b='qq';//新增一個記憶體空間存放'qq',b指向'qq' ``` 2. 創造階段、執行階段 1. 解析程式碼(編譯) 2. 丟給Chrome執行 ![](https://i.imgur.com/559kDH3.jpg) ## JS 函式與監聽 ### DOM(文件物件模型) #### 結構樹 ![](https://i.imgur.com/Edwkxw7.png) - 方便JS選取 - 方便CSS渲染、對應標籤 #### 透過JS選取 / 修改資料 ```javascript= document.querySelector(".header").textContent = "呵呵"; //選取該網頁文件,找到裡面的 .header,新增文字,賦予值 呵呵 document.querySelector(".header").textContent; // 抓出文字內容 var str = document.querySelector(".header").textContent; //將此文字內容當作變數的值 ``` #### 透過JS動態加入資料 ```javascript= document.querySelector(".header").classList.add('active') //在 .header上新增一 class,名稱為 active document.querySelector(".header").setAttribute('style','color:red') //增加屬性,(屬性名稱,值) ``` #### 透過JS賦予值 ```javascript= document.querySelector(".header").innerHTML='<h1>520</h1>'; //賦予值 使用= //document.querySelector(".header").innerHTML 為取得值 var _headerHTML = document.querySelector(".header").innerHTML; //將取出的值當作變數的值 //<h1>520</h1> ``` #### 將DOM放到記憶體空間(當作變數) ```javascript= var element = document.querySelector(".header"); element.innerHTML='<h1>520</h1>'; element.textContent = "呵呵"; //節省開發時間 ``` ### 函式(function) 宣告函式 ```javascript= function 函數名稱(參數){ 指令 } ``` 舉例:兩個數字加法計算機 - 找出變因 - 哪些東西會變來變去 - 要回傳什麼東西 ```javascript= function sum(num1,num2){ return num1+num2; } var a = sum(3,3);//a = 6 ``` - 函式會存放於記憶體中 - 但函式結果會當作一個(賦予變數的)新值>存放於新的記憶體 ![](https://i.imgur.com/beJCnV4.jpg) 參數 不等於 變數 ```javascript= function sum(num1,num2){ return num1+num2; //參數只活在此函式裡面,執行完即消失。 } console.log(num1);//找不到 ``` #### 區域變數 在函式裡設變數,則此變數只在韓式範圍內有效,執行完即消失。 例如: ![](https://i.imgur.com/QK85WZA.jpg) 上圖中,變數total只在紅框框內有效 ```javascript= console.log(friesPrice);//40 console.log(total);//找不到 ``` 而friesPrice與hambugerPrice為全域變數,即使在函式裡也可使用的變數。 ### 監聽 JS內建函式:.addEventListener ```javascript document.querySelector(".send").addEventListener("監聽動作", function(){ 觸發後效果 }) ``` 例如 ```javascript document.querySelector(".send").addEventListener("click", function(){ }) ``` 可使用Chrome > 檢查 >Event Listener 來看網頁元件的監聽事件是什麼 ![](https://i.imgur.com/rDRljnV.jpg) :::info 常見監聽事件 - onclick:當使用者產生點擊某元素時,例如選擇某的選項或是按鈕(button)。 - onchange:當元素發生改變時,例如選擇下拉選單(select option)中的其他項目時。 - onblur:當游標失去焦點時,也就是點選其他區域時,通常用於填完表單的一個欄位。 - ondblclick:連續兩次 click 某特定元素,通常用於需要特定確認的情況。 - onfocus:當網頁元素被鎖定的時候,例如 textarea、input text。 - onload:當頁面載入完成後立即觸發 function。 - onmousedown:滑鼠事件,當滑鼠的按鍵被按下的時候。 - onmouseover:滑鼠事件,當滑鼠游標移經某個元素或區塊時。 - onmousemove:滑鼠事件,當滑鼠游標移動時。 - onmouseout:滑鼠事件,當滑鼠移出某個元素或區塊時。 - onmouseup:滑鼠事件,當滑鼠的按鍵被放開的時候。 - onunload:當網友要準備離開網頁的時候。 :::

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully