未定义 JavaScript 方法

发布于 2024-09-07 19:40:16 字数 866 浏览 2 评论 0原文

当我将第一个脚本块中的代码(没有脚本标记本身)移动到外部 javascript 文件时,为什么这不起作用?

这个想法是从主页调用 homead.load() 并加载 homead_build() 处理的所有内容;

使用 Firefox 错误控制台时出现错误:'homead not Define'

<div id="box">
</div>

<script type="text/javascript">
    var homead = {
        box_id: "box",
        width: "150px",
        load: function(){
        homead.homead_build();
        },

        homead_build: function(){
            var div = document.createElement('div');
            div.style.width = homead.width;
            div.style.height = '55px';
            div.style.border = 'solid 2px #000';
            var box = document.getElementById(homead.box_id);
            box.appendChild(div);
        }
    };
</script>

<script language="JavaScript" type="text/javascript">

   homead.load();
</script>

Why does this not work when I move the code in the first script block (without the script tag itself) to an external javascript file.

The idea is to call homead.load() from home page and load everything processed by homead_build();

Error says 'homead not defined' using Firefox Error console

<div id="box">
</div>

<script type="text/javascript">
    var homead = {
        box_id: "box",
        width: "150px",
        load: function(){
        homead.homead_build();
        },

        homead_build: function(){
            var div = document.createElement('div');
            div.style.width = homead.width;
            div.style.height = '55px';
            div.style.border = 'solid 2px #000';
            var box = document.getElementById(homead.box_id);
            box.appendChild(div);
        }
    };
</script>

<script language="JavaScript" type="text/javascript">

   homead.load();
</script>

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

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

发布评论

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

评论(3

我不在是我 2024-09-14 19:40:16

也许您在调用 homead.load() 后包含了外部脚本文件?使用前请确保已加载:

<script language="JavaScript" type="text/javascript" src="external.js"></script>
<script language="JavaScript" type="text/javascript">

   homead.load();
</script>

Maybe you are including the external script file after you call homead.load()? Make sure it is loaded before it is used:

<script language="JavaScript" type="text/javascript" src="external.js"></script>
<script language="JavaScript" type="text/javascript">

   homead.load();
</script>
眼趣 2024-09-14 19:40:16

确保在使用 homead 之前包含它,并且该文件已正确加载。
(例如:在文件中说alert(“im returned...”))

顺便说一句,您的代码可以简化:

var homead = {
    box_id: "box",
    width: "150px",
    load: function(){
        var div = document.createElement('div');
        div.style.width = homead.width;
        div.style.height = '55px';
        div.style.border = 'solid 2px #000';
        var box = document.getElementById(homead.box_id);
        box.appendChild(div);
    }
};

Make sure you include it before using homead, and that the file has been loaded correctly.
(e.g.: say alert("im loaded...") in the file)

Your code can be simplified btw:

var homead = {
    box_id: "box",
    width: "150px",
    load: function(){
        var div = document.createElement('div');
        div.style.width = homead.width;
        div.style.height = '55px';
        div.style.border = 'solid 2px #000';
        var box = document.getElementById(homead.box_id);
        box.appendChild(div);
    }
};
妄司 2024-09-14 19:40:16

是的,它工作正常。您可能向脚本标记提供了错误的文件 src 路径。

我已经做了以下工作;

 <script language="JavaScript" type="text/javascript" src="../../Scripts/test.js" ></script>

添加了代码

    <div id="box">
</div>


<script language="JavaScript" type="text/javascript">

   homead.load();
</script>

yes its working fine.You might be giving wrong src path of file to script tag.

I have done following ;

 <script language="JavaScript" type="text/javascript" src="../../Scripts/test.js" ></script>

added code

    <div id="box">
</div>


<script language="JavaScript" type="text/javascript">

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