Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <div class="group" id="group">
<select name='options1'>
</select>
<select name='options2'>
</select>  
<select name='options3'>
</select>
 
</div>
            
          
!
            
              var count = 0;
// limit - 列表的长度
// deep - 数据的层级数
// 返回一个测试数据列表
var genList = function(limit, deep) {
  var ret = [];
  if (!deep) {
    return ret;
  }
  for (var i = 0, it; i < limit; i++) {
    it = deep + '.' + i + '.' + (count++);
    ret.push({
      text: it,
      value: it,
      list: genList(limit, deep - 1)
    });
  }
  return ret;
};

function cascade(selectList, data) {
  // 取第n级的数据列表,这个很多同学也已经完成了,非常好
  var getList = function(n) {
    var ret = data;
    for (var i = 0, it; i < n; i++) {
      // 默认选中第一项
      it = Math.max(0, selectList[i].selectedIndex);
      ret = (ret[it] || {}).list;
    }
    return ret;
  };

  // 选择器填充,这个在课程里已经有介绍
  var fillSelector = function(select, list) {
    for (var i = select.length - 1; i >= 0; i--) {
      select.remove(i);
    }
    if (!list || !list.length) {
      return;
    }
    for (var i = 0, l = list.length, it; i < l; i++) {
      it = list[i];
      select.add(new Option(it.text, it.value));
    }
  };

  // 第几级选择器变化事件
  var onSelectChange = function(i) {
    // 1. 填充第i+1级选择器
    // 2. 触发第i+1级选择器的change逻辑
    var next = i + 1;
    if (next >= selectList.length) {
      return;
    }
    fillAndChange(next);
  };
  // 填充第几级选择器并触发该级选择器的change逻辑
  var fillAndChange = function(index) {
    console.log(index);
    fillSelector(
      selectList[index],
      getList(index)
    );
    onSelectChange(index);
  };

  // 事件辅助函数,保存索引,这个在JS课程中已经介绍
  var helper = function(index) {
    return function() {
      onSelectChange(index);
    };
  };
  for (var i = 0, l = selectList.length - 1; i < l; i++) {
    // 这里可以实践一下前面学的闭包的知识
    // 或者针对高版本用bind,如 onSelectChange.bind(null,i)
    selectList[i].onchange = helper(i);
  }

  // 初始化数据
  fillAndChange(0);
}

var data = genList(3, 3);
var selectList = document.getElementById('group').children;
cascade(selectList, data);
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console