onmousemove事件不会被解雇

时间:2013-03-21 01:54:55

标签: javascript html onmouseover

我在没有任何解决方案的情况下搜索了此博客和其他人提供的答案。非常感谢帮助。

我有一个onmousemove事件,它正在执行要触发的事件列表中的第一个项目但不会执行第二个项目。             document.getElementById("map_image_africa").onmousemove = maps.redraw; learnPanel.redraw;

该问题似乎不是特定于浏览器的;它出现在Chrome和Safari中。还没试过firefox。

在Chrome中,我可以启动浏览器的控制台并执行learnPanel.redraw();,它可以按预期工作。 此外,如果我在onmouseover事件中的maps.redraw和learnPanel.redraw语句之间放置一个警告语句,警报将按预期执行。

使用以下代码创建learnPanel对象

        LearnPanel.prototype = new Panel();
        LearnPanel.prototype.constructor = LearnPanel;
        function LearnPanel() {
        }
        LearnPanel.prototype.draw = function() {

            var br_element = document.createElement("br");


            /* country name */
            if(this.isCountryNamesSelected === "yes") {
                var temp_label;
                temp_label = document.createElement("label");
                temp_label.classList.add('quiz_question_heading_label');
                temp_label.innerHTML = "Country: ";

                this.flagColumnTab.appendChild(temp_label);

                this.labelCountryName = document.createElement("label");
                this.labelCountryName.id = "country_name";
                this.labelCountryName.classList.add('quiz_question_label');

                this.flagColumnTab.appendChild(this.labelCountryName);
                this.flagColumnTab.appendChild(document.createElement("br"));

            }
            /* capital */
            if(this.isCapitalNamesSelected === "yes") {
                var temp_label;
                temp_label = document.createElement("label");
                temp_label.classList.add('quiz_question_heading_label');
                temp_label.innerHTML = "Capital: ";

                this.flagColumnTab.appendChild(temp_label);

                this.labelCapitalName = document.createElement("label");
                this.labelCapitalName.id = "capital";
                this.labelCapitalName.classList.add('quiz_question_label');

                this.flagColumnTab.appendChild(this.labelCapitalName);
                this.flagColumnTab.appendChild(document.createElement("br"));

            }
            /* flag */
            if(this.isCountryFlagsSelected === "yes") {

                this.flagImage = document.createElement("img");
                this.flagImage.id = "flag_image";
                this.flagImage.src = "/images/flags/_flag.jpeg";
                this.flagImage.alt = "flag logo";

                this.flagColumnTab.appendChild(this.flagImage);

                this.flagColumnTab.appendChild(document.createElement("br"));
                this.flagColumnTab.appendChild(document.createElement("br"));

            }


        }
        LearnPanel.prototype.redraw = function() {


            alert('redrawing learnPanel');
            /* country name */
            if(learnPanel.isCountryNamesSelected === "yes") {
                learnPanel.labelCountryName.innerHTML = maps.getCurrentDisplayCountryDown();
            }
            /* capital */
            if(learnPanel.isCapitalNamesSelected === "yes") {
                learnPanel.labelCapitalName.innerHTML = maps.getCurrentCountryCapitalDown();
            }
            /* flag */
            if(learnPanel.isCountryFlagsSelected === "yes") {
                learnPanel.flagImage.src = "/images/flags/" + maps.getCurrentFlagDown() + "_flag.jpeg";
                learnPanel.flagImage.alt = "flag of " + maps.getCurrentFlagDown();
            }
            /* head of state */
            /* famous landmark */

        }


        learnPanel = new LearnPanel();

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:6)

欢迎使用StackOverflow!

您需要的是匿名函数。在mousemove上为#map_large_africa属性分配函数时,您希望发生两个不同的函数。不幸的是,由于您编写以下声明的方式:

`document.getElementById("map_image_africa").onmousemove = maps.redraw; learnPanel.redraw;`

实际上是这样解析的:

document.getElementById("map_image_africa").onmousemove = maps.redraw;

learnPanel.redraw;

你看到了问题吗?分号充当命令的结尾。在mousemove上重新绘制地图,但learnPanel.redraw不属于onmousemove赋值,并且该行仅在onmousemove属性分配给#map_image_africa之后执行一次。通过将其包装在anonymous function中,您可以一次执行多行javascript。

尝试以下方法:

document.getElementById("map_image_africa").onmousemove = function() {
    maps.redraw();    // note that these lines have () after the method name.
    learnPanel.redraw();
}

虽然我有你,但请注意,不建议直接修改onmousemoveonclick等属性, Events 是与DOM互动的首选方式元素,因为它将javascript与HTML分开。

上述onmousemove作业可以(并且应该)重写:

document.getElementById("map_image_africa").addEventListener('mousemove', function() {
    maps.redraw();
    learnPanel.redraw();
}, false);