6

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

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

该问题似乎不是特定于浏览器的;它发生在 Chrome 和 Safari 中。还没试过火狐。

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

在此先感谢您的帮助!

4

1 回答 1

6

欢迎来到 StackOverflow!

你需要的是一个匿名函数。当您为mousemoveon 属性分配函数时#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之后执行一次。通过将其包装在匿名函数中,您可以一次执行多行 javascript。onmousemove#map_image_africa

尝试以下操作:

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

虽然我有你,但请注意不鼓励直接修改onmousemove和之类的属性,事件是与 DOM 元素交互的首选方式,因为它将 javascript 与 HTML 分开。onclick

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

document.getElementById("map_image_africa").addEventListener('mousemove', function() {
    maps.redraw();
    learnPanel.redraw();
}, false);
于 2013-03-21T02:01:14.367 回答