函数未被调用

发布于 2024-11-30 11:06:42 字数 2581 浏览 4 评论 0原文

我有一些 javascript 应该在窗口加载后运行,但由于某种原因,它永远不会运行。

这是我的代码:

function setClasses(){
        document.getElementsByClassName("gchoice_35_0")[0].onclick = sedanShow;
        document.getElementsByClassName("gchoice_22_0")[0].onclick = sedanShow;
        document.getElementsByClassName("gchoice_34_0")[0].onclick = sedanShow;

        document.getElementsByClassName("gchoice_34_1")[0].onclick = suvShow;
        document.getElementsByClassName("gchoice_35_1")[0].onclick = suvShow;
        document.getElementsByClassName("gchoice_22_1")[0].onclick = suvShow;

        document.getElementsByClassName("gchoice_22_2")[0].onclick = vanShow;
        document.getElementsByClassName("gchoice_35_2")[0].onclick = vanShow;
        document.getElementsByClassName("gchoice_34_2")[0].onclick = vanShow;
    }

window.onload = setClasses;

setClasses() 函数似乎没有运行。然而,当我通过 FireBug 控制台手动调用它时,它确实有效。

该代码放置在我的网页的标题中。

任何帮助表示赞赏。

完整的 html 片段:

<head>
......

<script type="text/javascript">
function setClasses(){
        document.getElementsByClassName("gchoice_35_0")[0].onclick = sedanShow;
        document.getElementsByClassName("gchoice_22_0")[0].onclick = sedanShow;
        document.getElementsByClassName("gchoice_34_0")[0].onclick = sedanShow;

        document.getElementsByClassName("gchoice_34_1")[0].onclick = suvShow;
        document.getElementsByClassName("gchoice_35_1")[0].onclick = suvShow;
        document.getElementsByClassName("gchoice_22_1")[0].onclick = suvShow;

        document.getElementsByClassName("gchoice_22_2")[0].onclick = vanShow;
        document.getElementsByClassName("gchoice_35_2")[0].onclick = vanShow;
        document.getElementsByClassName("gchoice_34_2")[0].onclick = vanShow;
    }

    function sedanShow(){
            document.getElementById("sedan").style.display="inline"
            document.getElementById("suv").style.display="none"
            document.getElementById("van").style.display="none"
        }
        function suvShow(){
            document.getElementById("sedan").style.display="none"
            document.getElementById("suv").style.display="inline"
            document.getElementById("van").style.display="none"
        }
        function vanShow(){
            document.getElementById("sedan").style.display="none"
            document.getElementById("suv").style.display="none"
            document.getElementById("van").style.display="inline"
        }
    window.onload = setClasses;
    </script>

......

I have some javascript that is supposed to run after the window loads but for some reason, it never runs.

Here's my code:

function setClasses(){
        document.getElementsByClassName("gchoice_35_0")[0].onclick = sedanShow;
        document.getElementsByClassName("gchoice_22_0")[0].onclick = sedanShow;
        document.getElementsByClassName("gchoice_34_0")[0].onclick = sedanShow;

        document.getElementsByClassName("gchoice_34_1")[0].onclick = suvShow;
        document.getElementsByClassName("gchoice_35_1")[0].onclick = suvShow;
        document.getElementsByClassName("gchoice_22_1")[0].onclick = suvShow;

        document.getElementsByClassName("gchoice_22_2")[0].onclick = vanShow;
        document.getElementsByClassName("gchoice_35_2")[0].onclick = vanShow;
        document.getElementsByClassName("gchoice_34_2")[0].onclick = vanShow;
    }

window.onload = setClasses;

The setClasses() function doesn't seem to run. It does however work when I manually call it through the console of FireBug.

The code is placed in the header of my web page.

Any help is appreciated.

Full html snippet:

<head>
......

<script type="text/javascript">
function setClasses(){
        document.getElementsByClassName("gchoice_35_0")[0].onclick = sedanShow;
        document.getElementsByClassName("gchoice_22_0")[0].onclick = sedanShow;
        document.getElementsByClassName("gchoice_34_0")[0].onclick = sedanShow;

        document.getElementsByClassName("gchoice_34_1")[0].onclick = suvShow;
        document.getElementsByClassName("gchoice_35_1")[0].onclick = suvShow;
        document.getElementsByClassName("gchoice_22_1")[0].onclick = suvShow;

        document.getElementsByClassName("gchoice_22_2")[0].onclick = vanShow;
        document.getElementsByClassName("gchoice_35_2")[0].onclick = vanShow;
        document.getElementsByClassName("gchoice_34_2")[0].onclick = vanShow;
    }

    function sedanShow(){
            document.getElementById("sedan").style.display="inline"
            document.getElementById("suv").style.display="none"
            document.getElementById("van").style.display="none"
        }
        function suvShow(){
            document.getElementById("sedan").style.display="none"
            document.getElementById("suv").style.display="inline"
            document.getElementById("van").style.display="none"
        }
        function vanShow(){
            document.getElementById("sedan").style.display="none"
            document.getElementById("suv").style.display="none"
            document.getElementById("van").style.display="inline"
        }
    window.onload = setClasses;
    </script>

......

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

灰色世界里的红玫瑰 2024-12-07 11:06:42

您始终可以使用 JQuery

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>

<script type="text/javascript">
        $(document).ready(function() {
            setClasses();
        });
</script>

编辑

示例 JQuery 事件重构:

$(".gchoice_35_0").click(function(){
    //Handler Code...
});

You could always use JQuery

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>

<script type="text/javascript">
        $(document).ready(function() {
            setClasses();
        });
</script>

Edit

Example JQuery Event Refactor:

$(".gchoice_35_0").click(function(){
    //Handler Code...
});
温柔戏命师 2024-12-07 11:06:42

我将给出一个使用 YUI 的答案,请随意使用或不使用它,但我认为框架是帮助加快 javascript 开发的好主意。因此,将以下内容添加到您的页面中

<script type="text/javascript" src="http://yui.yahooapis.com/2.9.0/build/yahoo-dom-event/yahoo-dom-event.js"></script>

,然后执行以下操作,而不是 window.onload = someFn (因为这在 IE 中可能很奇怪,令人惊讶),

YAHOO.util.Event.onDOMReady(function () {
    setClasses();
});

然后在您的设置类函数中执行以下操作,

function setClasses(){
        YAHOO.util.Event.on(document.getElementsByClassName("gchoice_35_0")[0], 'click', sedanShow);
        YAHOO.util.Event.on(document.getElementsByClassName("gchoice_22_0")[0], 'click', sedanShow);
        YAHOO.util.Event.on(document.getElementsByClassName("gchoice_34_0")[0], 'click', sedanShow);

        YAHOO.util.Event.on(document.getElementsByClassName("gchoice_34_1")[0], 'click', suvShow);
        YAHOO.util.Event.on(document.getElementsByClassName("gchoice_35_1")[0], 'click', suvShow);
        YAHOO.util.Event.on(document.getElementsByClassName("gchoice_22_1")[0], 'click', suvShow);

        YAHOO.util.Event.on(document.getElementsByClassName("gchoice_22_2")[0], 'click', vanShow);
        YAHOO.util.Event.on(document.getElementsByClassName("gchoice_35_2")[0], 'click', vanShow);
        YAHOO.util.Event.on(document.getElementsByClassName("gchoice_34_2")[0], 'click', vanShow);
    }

这应该可以解决问题。

I am going to give an answer that uses YUI, feel free to use it or not, but I think frameworks are a good idea to help speed up javascript development. So, add the following in to your page

<script type="text/javascript" src="http://yui.yahooapis.com/2.9.0/build/yahoo-dom-event/yahoo-dom-event.js"></script>

Then instead of window.onload = someFn (because this can be quirky in IE, surprise surprise), do the following

YAHOO.util.Event.onDOMReady(function () {
    setClasses();
});

Then in your set classes function, do the following

function setClasses(){
        YAHOO.util.Event.on(document.getElementsByClassName("gchoice_35_0")[0], 'click', sedanShow);
        YAHOO.util.Event.on(document.getElementsByClassName("gchoice_22_0")[0], 'click', sedanShow);
        YAHOO.util.Event.on(document.getElementsByClassName("gchoice_34_0")[0], 'click', sedanShow);

        YAHOO.util.Event.on(document.getElementsByClassName("gchoice_34_1")[0], 'click', suvShow);
        YAHOO.util.Event.on(document.getElementsByClassName("gchoice_35_1")[0], 'click', suvShow);
        YAHOO.util.Event.on(document.getElementsByClassName("gchoice_22_1")[0], 'click', suvShow);

        YAHOO.util.Event.on(document.getElementsByClassName("gchoice_22_2")[0], 'click', vanShow);
        YAHOO.util.Event.on(document.getElementsByClassName("gchoice_35_2")[0], 'click', vanShow);
        YAHOO.util.Event.on(document.getElementsByClassName("gchoice_34_2")[0], 'click', vanShow);
    }

That should do the trick.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文