我搜索了此博客和其他博客中提供的答案,没有任何解决方法。非常感谢您的帮助。

我有一个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();

先谢谢您的帮助!

最佳答案

欢迎来到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上重绘了 map ,但是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);

08-03 15:44