Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                h1 JavaScript DOM Manipulation
hr
h5 參考文章
ul
    li #[a(href="https://www.sitepoint.com/dom-manipulation-vanilla-javascript-no-jquery/") The Basics of DOM Manipulation in Vanilla JavaScript (No jQuery)] @ sitePoint
    li #[a(href="https://www.ptt.cc/bbs/Ajax/M.1491563311.A.1F2.html") 中譯-都2017年了  學學用原生JS來操作DOM吧]
h3.title-border 選取元素
p #[span.code document.querySelector('cssSelector')]
p #[span.code document.querySelectorAll('cssSelector')]
pre
    code.language-js
        | // 透過 Array.from(els) 將 array-like Nodelist 轉成陣列,以使用 forEach 方法
        | const els = doucment.querySelectorAll('p')
        | Array.from(els).forEach(...)  
hr
h3.title-border 插入元素
pre 
    code.language-js
        | const tagP = document.createElement('p')        // <p></p>
        | tagP.textContent = 'This is paragraph'
        | const targetCreateElement = document.querySelector('.target-createElement')
        | targetCreateElement.appendChild(tagP)
h5 建立元素 
p #[span.code document.createElement('tagName')]
h5 添加元素
p #[span.code el.appendChild()]
.target-createElement
hr
h3.title-border 插入元素內容
h5 在元素多處插入內容
p #[span.code el.insertAdjacentHTML(position, text)]
pre
    code.language-js
        | // position 有 4 種,分別為 beforebegin, afterbegin, beforeend, afterend
        | el.insertAdjacentHTML(position, text)
p.target-insertAdjacentHTML I am a P tag Target.
h5 在元素中插入內容
p #[span.code el.textContent]
pre
    code.language-js 
        | /* el.textContent */
        | const textContent = el.textContent  // 取得元素內容
        | el.textContent = 'Updated Content'  // 變更元素內容
p.target-textContent I am a P tag Target.
hr
h3 修改元素屬性(attribute)
p #[span.code el.value]
p 元素屬性的修改只需要直接給值就可以,如果要去除該元素,就設定成 #[span.code null]
pre
    code.language-js
        | // 取得屬性
        | const oneValue = oneElement.value
        | 
        | // 設定屬性
        | oneElement.value = 'hello'    
        | 
        | // 一口氣設定好多種屬性,用 Object.assign()
        | Object.assign(oneElement. {
        |     value: 'hello',
        |     id: 'world'
        | })
        | 
        | // 要刪除屬性,設定成 null 就好
        | oneElement.value = null
div
    input(type="text" id="target-input" value="Hello")
    button(id="change-input-value") Change Value
    p#target-input-value
hr
h3 操作 DOM 元素
h5 插入 DOM 元素
p #[span.code  element1.appendChild(element2)]
p #[span.code  element1.insertBefore(element2, element3)]
pre
    code.language-js
        | // 在 element1 裡插入一個 element2
        | element1.appendChild(element2)
        | 
        | // 在 element1 裡的 element3 之前插入一個 element2
        | element1.insertBefore(element2, element3)
        | 
        | // 沒有 insertAfter !!
        | // 在 element1 裡的 element3 「之後」插入一個 element2
        | element1.insertBefore(element2, element3.nextSibling)
        | // 不能寫成:element1.insertAfter(element2, element3)
h5 複製 DOM 元素
p #[span.code   const newElement = oneElement.cloneNode()]
              
            
!

CSS

              
                // variable direct ues in scss
$green: #41CEC0;
$vue: #41b883;
$blue: #35495e;
$purple: #A53860;
$red: #C63647;
$yellow: #FFDF2C;
$orange: #E96900;
              
            
!

JS

              
                /**
 * document.createElement('tagName')
 **/
const tagP = document.createElement('p')        // <p></p>
tagP.textContent = 'This is paragraph'
const targetCreateElement = document.querySelector('.target-createElement')
targetCreateElement.appendChild(tagP)


/**
 * el.insertAdjacentHTML(position, text)
 **/
const targetInsertAdjacentHTML = document.querySelector('.target-insertAdjacentHTML')

targetInsertAdjacentHTML.insertAdjacentHTML('beforebegin', '<--beforebegin-->')
targetInsertAdjacentHTML.insertAdjacentHTML('afterbegin', '<--afterbegin-->')
targetInsertAdjacentHTML.insertAdjacentHTML('beforeend', '<--beforeend-->')
targetInsertAdjacentHTML.insertAdjacentHTML('afterend', '<--afterend-->')

/**
 * el.textContent
 **/

const targetTextContent = document.querySelector('.target-textContent')
// console.log(targetTextContent.textContent)
targetTextContent.textContent = 'Content Updated'

/**
 * 改變元素屬性 el.value
**/
const targetInput = document.getElementById('target-input')
const targetInputValue = document.querySelector('#target-input-value')
const changeInputValue = document.querySelector('#change-input-value')
changeInputValue.addEventListener('click', () => {
    targetInput.type = "date"
    targetInput.value = new Date().toISOString().split('T')[0]  
})
targetInputValue.textContent = targetInput.value
targetInput.addEventListener('change', (e) => {
      targetInputValue.textContent = e.target.value
})

              
            
!
999px

Console