未定义 JavaScript 方法
当我将第一个脚本块中的代码(没有脚本标记本身)移动到外部 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许您在调用
homead.load()
后包含了外部脚本文件?使用前请确保已加载:Maybe you are including the external script file after you call
homead.load()
? Make sure it is loaded before it is used:确保在使用 homead 之前包含它,并且该文件已正确加载。
(例如:在文件中说alert(“im returned...”))
顺便说一句,您的代码可以简化:
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:
是的,它工作正常。您可能向脚本标记提供了错误的文件 src 路径。
我已经做了以下工作;
添加了代码
yes its working fine.You might be giving wrong src path of file to script tag.
I have done following ;
added code