簡體   English   中英

帶參數的Javascript事件處理程序

[英]Javascript event handler with parameter

我正在嘗試為svg元素設置onmousemove事件。 下面的代碼在chrome中有效,但在firefox中,並非每次都正確設置onmousemove屬性。 第二次更改后,它變得混亂了。

mysvg.setAttribute('onmousemove','window.updateXY(evt,' + that.chartNumber + ')');

根據螢火蟲,mysvg仍然是正確的svg元素:svg#chart0

svg屬性onmousemove在混亂時會說用函數(e)而不是onmousemove(evt),但是在螢火蟲中未報告任何錯誤

編輯以顯示更多代碼:

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();
            }

}

我嘗試了上述所有方法,但在Firefox中都無法正常工作。 解決方案是不將event以外的任何參數傳遞給updateXY函數。 當其他參數未定義時,請運行單獨的邏輯以確定在這種情況下應設置的參數。 一旦代碼位於window框架中,就可以setAttribute使用setAttribute #FirefoxOOP失敗

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM