如何在两个函数之间调用变量

时间:2013-06-07 07:02:29

标签: javascript paperjs

我需要澄清我收到错误未捕获的ReferenceError:cir未定义我可以确定错误原因,但我无法解决。任何人都可以帮助我

    function onMouseUp(event) {i++;
                    circles.push(circle);
                    var cir = new Group();
                    cir.addChild(circle);
                    createElem('circle', i);
}

这是一个创建元素的函数,

function createElem(id, i){
            var exist = document.getElementById('markList');
            c1 = document.createElement('div');
            var text = document.createTextNode(id+" "+i);
            c1.appendChild(text);
            c1.setAttribute('id', id+'_'+i);
            c1.onclick = function (){
                cir.removeChildren(i, i+1);

            };
            exist.appendChild(c1);  
        }

1 个答案:

答案 0 :(得分:0)

cir传递给createElem,功能.....

function onMouseUp(event) {i++;
                    circles.push(circle);
                    cir = new Group();
                    cir.addChild(circle);
                    createElem('circle', i,cir);
}

function createElem(id, i,cir){
            var exist = document.getElementById('markList');
            c1 = document.createElement('div');
            var text = document.createTextNode(id+" "+i);
            c1.appendChild(text);
            c1.setAttribute('id', id+'_'+i);
            c1.onclick = function (){
                cir.removeChildren(i, i+1);

            };
            exist.appendChild(c1);  
        }

编辑:演示

<script>
onMouseUp();
function onMouseUp() {

                    var cir = 1;
                    var i=1;
                    createElem('circle', i,cir);
}
function createElem(id, i,cir){
         var  fun=function (){
                alert(cir);
            };
            fun();
        }
 </script>