Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ 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

Auto Save

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

              
                	<ul class="list"></ul>

              
            
!

CSS

              
                .list {
  padding: 50px;
  background-color: #333;
  color: #fff;
}
              
            
!

JS

              
                //JSON
var shops = [{
  keeper: '魯夫'
}, {
  keeper: '香吉士'
}]
//綁定 .list,使用父元素控制子元素
var list = document.querySelector('.list');

//為了把資料回填到 li 中,故透過這個函式將資料抓出後渲染到網頁上
//更新商店資料
function updateList() {
  var str = ''; //組一個字串,內容為空
  var shopLen = shops.length; //資料長度 2
  for (var i = 0; shopLen > i; i++) {
    str += `<li data-num=${i}>${shops[i].keeper}</li>`
  } //字串加總
  list.innerHTML = str; //渲染到字串,並渲染到網頁
  console.log(str);
}
updateList(); //執行函式


// 確認點擊的店長是誰
function checkList(e) {
  var num = e.target.dataset.num; //宣告一個 num ,當事件目標為自己,dataset 取出 num 的值
  console.log(e.target.nodeName); //確認 nodeName 是否為 'LI'
  if (e.target.nodeName !== 'LI') { //若選到的 nodeName 不是 LI,就回傳(停止函式)
    return
  };
  console.log(`你選擇的店長為 ${shops[num].keeper}`); //顯示選擇內容
}

//在 list 新增 click 事件
list.addEventListener('click', checkList, false);
              
            
!
999px

Console