too much recursion

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        label{
            color: rgba(231, 117, 231, 0.91);
        }
        select{
            width: 100px;
            color: blue;
        }
    </style>
</head>
<body>
    <label for="">省份</label>
    <select name="" id="province" onchange="onchange()">
        <option value=""id="op">请选择省份</option>
        <option value="" id="op1" >四川</option>
        <option value="" id="op2" >重庆</option>
        <option value="" id="op3" >山东</option>
    </select>
    <br/><br/>
    <label for="">城市</label>
    <select name="" id="city"></select>

    <script>
        var province = document.getElementById("province");
        var city = document.getElementById("city");
        var op = document.getElementById("op");
        op.selected=true;
        function onchange(){
            if(province.selectedIndex==1){
                for(var i=city.children.length;i>=0;i--){
                    city.remove(i);
                }
                var allCity = ["成都","南充","绵阳"];
                for(var i =0;i<allCity.length;i++ ){
                    city.options.add(new Option(allCity[i],allCity[i]));
                }
            }else if(province.selectedIndex==2){
                for(var i=city.children.length;i>=0;i--){
                    city.remove(i);
                }
                var allCity= ["万州","丰都","九龙坡"]
                for(var i =0;i<allCity.length;i++ ){
                    city.options.add(new Option(allCity[i],allCity[i]));
                }

            }else if(province.selectedIndex==3){
                for(var i=city.children.length;i>=0;i--){
                    city.remove(i);
                }
                var allCity =["济南","青岛","烟台"];
                for(var i=0;i<allCity.length;i++){
                    city.options.add(new Option(allCity[i],allCity[i]));
                }
            }
        }
    </script>
</body>
</html>

 

运行这个JS在FF中会出现too much recursion错误,而在IE中会出现Stack Overflow错误,
困惑我不少时间,经过搜索得知:
原来JS的关键字不可以用来作为函数名的。
所以解决方法是:把onchange函数名改为其他不是关键字的就行了。

posted on 2016-07-21 19:44  tassle  阅读(286)  评论(0编辑  收藏  举报