cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

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.

            
              .demo-append-child
    h1 DEMO Node.appendChild()
    button(type="button" id="appendNode") 切換 appendChild
    button(type="button" id="cloneNode") 添加 cloneNode
    hr
    .block.block-1
    .block.block-2
    .block.block-3
            
          
!
            
              html, body {
    // margin: 30px;
    padding: 30px;
}
// variable direct ues in scss
$vue: #41b883;
$dark-blue: #3d4f5d;
$wesbos: #ffc600;

hr{
    margin: 20px 0;
}
.block {
    width: 100px;
    height: 100px;
    display: inline-block;
    vertical-align: middle;
    margin: 0px 10px;
    background-color: $dark-blue;
    padding: 50px;
}

button {
    margin: 10px 0;
    padding: 5px;
}
button+button{
    margin-left: 10px;
}

.outer, .inner {
    color: $wesbos;
}
.outer {
    padding: 50px;
    background-color: $dark-blue;
}
.inner {
    padding: 50px;
    background-color: $vue;
}

            
          
!
            
              /******************
 *     DEMO-1     *
 *****************/
// 利用 document.createElement 建立 DOM Element
let innerElement = document.createElement('div')
innerElement.classList.add('inner')

// 分別選擇各個 block
const block1 = document.querySelector('.block-1')
const block2 = document.querySelector('.block-2')
const block3 = document.querySelector('.block-3')

let i = 0
const buttonAppend = document.querySelector('#appendNode')
buttonAppend.addEventListener('click', function(){
    console.log(i)
    if (i === 0) {
        block1.appendChild(innerElement)    
    }  else if (i === 1) {
        block2.appendChild(innerElement)
    } else {
        block3.appendChild(innerElement)
    }
    i = (i + 1) % 3
})


const buttonClone = document.querySelector('#cloneNode')
buttonClone.addEventListener('click', function () {
    let cloneElement = innerElement.cloneNode(true)
     if (i === 0) {
        block1.appendChild(cloneElement)    
    }  else if (i === 1) {
        block2.appendChild(cloneElement)
    } else {
        block3.appendChild(cloneElement)
    }
    i = (i + 1) % 3
})

            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console