html5中js添加下拉菜单

<select id="first" οnchange="change()"></select><br/>
<ul id="caidan"></ul>

<script>
    var first = document.getElementById('first');
    var caidan = document.getElementById("caidan");

    function init() {
        var arr_first = ['请选择您要吃哪种','凉菜', '热菜', '招牌菜','主食','酒水'];
       // arr_first.push('北京');
        var ops = '';
        for (var i = 0; i < arr_first.length; i++) {
            ops = ops + '<option>' + arr_first[i] + '</option>';
        }
        first.innerHTML = ops;

    }init();

    var caidan_arr = [
        ['果仁花生', '拍黄瓜', '肉丝拉皮', '海带丝'],
        ['宫保鸡丁', '清蒸鲈鱼', '油炸小黄鱼'],
        ['金银馒头', '黄金米饭'],
        ['水饺','刀削面'],
        ['十年果粒橙' ,'百年啤酒','千年白酒','万年泔水']
    ]


    function _clear( _select ) {
        /*if(_select.length>0) {
         /!*for(var i=_select.length-1 ; i>=0 ; --i) {
         _select.options.remove(i);
         }*!/
         while(_select.length>0) {
         _select.options.remove(0);
         }
         }*/
        _select.innerHTML = '';
    }

    function addli( txt ) {
        var _li = document.createElement('li');
        _li.textContent = txt;
        caidan.appendChild(_li);
    }

    function change() {
       // alert(first.textContent);
        _clear(caidan);
        var index = first.selectedIndex;
        if(index ==0) {
            return;
        }
        else {
            for (var i = 0; i < caidan_arr[index - 1].length; i++) {
                addli(caidan_arr[index - 1][i]);
            }
        }
    }
</script>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值