带参数的Javascript事件处理程序

时间:2015-04-04 21:07:00

标签: javascript svg

我正在尝试为svg元素设置onmousemove事件。下面的代码适用于chrome,但在firefox中,它并不是每次都正确设置onmousemove属性。在第二次更改后,它变得混乱。

mysvg.setAttribute('onmousemove','window.updateXY(evt,' + that.chartNumber + ')');
根据firebug,mysvg仍然是正确的svg元素:svg#chart0

svg属性onmousemove表示函数(e)而不是onmousemove(evt),当它搞砸了,但是在firebug中没有报告错误

编辑以显示更多代码:

var YAxis = function(mysvg,chartNumber){
            this.chartNumber = chartNumber;
            var that = this;
            var g = document.createElementNS(xmlns, "g");
            this.id = 'YAxis' + chartNumber;
            g.id = this.id;
            mysvg.appendChild(g);
            var shift = false;
            this.selected = false;


            this.yScale = function(e){
                e.stopPropagation();
                that.origY = e.pageY;
                that.shift = e.ctrlKey;
                mysvg.onmousemove = that.dragY;
                mysvg.onmouseup = that.dropY;
            }

            this.dragY = function(e) {
                var chart = charts[that.chartNumber];
                if(chart.log)
                    displayXYText.innerHTML = 'range: ' + Math.round(Math.pow(10,chart.max*(1 + (e.pageY - that.origY)/(chart.height)))*100)/100  + '-' + Math.round(Math.pow(10,chart.min*(1 - (e.pageY - that.origY)/(chart.height)))*100)/100;
                else
                    displayXYText.innerHTML = 'range: ' + Math.round(chart.max*(1 + (e.pageY - that.origY)/(chart.height))*100)/100  + '-' + Math.round(chart.min*(1 - (e.pageY - that.origY)/(chart.height))*100)/100;
            }
            this.dropY = function(e) {
                var chart = charts[that.chartNumber];
                if(that.shift){
                    var range = (chart.max - chart.min)*Math.round((e.pageY - that.origY)/(chart.height)*1000)/1000;
                    chart.max += range;
                    chart.min += range;
                }else{
                    var range = chart.max - chart.min;
                    chart.max *= 1 + Math.round((e.pageY - that.origY)/(chart.height)*1000)/1000;
                    chart.min *= 1 - Math.round((e.pageY - that.origY)/(chart.height)*1000)/1000;
                    if(chart.max <= chart.min){
                        chart.max = chart.min + range;
                    }
                }


                mysvg.setAttribute('onmousemove','window.updateXY(event,' + that.chartNumber + ')');
                //mysvg.onmousemove = updateXY(e,that.chartNumber);//function() { updateXY(e,that.chartNumber)}; //set back to orig function
                //mysvg.setAttribute('onmousemove','updateXY(evt,' + that.chartNumber + ')');
                //console.debug(mysvg.onmousemove.textContent);

                mysvg.setAttribute('onmouseup','window.mouseUp(event)');
                //console.debug("got here onmousemove");
                chart.redraw();
            }

}

1 个答案:

答案 0 :(得分:0)

我尝试了上面的所有方法但没有在firefox中正常工作。解决方案是不将event以外的任何参数传递给updateXY函数。当其他参数未定义时,运行单独的逻辑以确定在这种情况下参数应该是什么。代码位于window框架后,您可以像平常一样使用setAttribute。 #FirefoxOOPfail

相关问题