var test=1;1.在页面中直接写出来,可以alert(test)出来,
2.保存在外部1.js文件中,在页面中进行引用也可以alert(test)出来3.
用a=document.createElement('script');
a.setAttribute("type","text/javascript");
a.src="1.js"; //1.js即外部JS
document.body.appendChild(a);这种方式就不能调用test了
向大家寻求一下问题所在。谢谢你们了。
2.保存在外部1.js文件中,在页面中进行引用也可以alert(test)出来3.
用a=document.createElement('script');
a.setAttribute("type","text/javascript");
a.src="1.js"; //1.js即外部JS
document.body.appendChild(a);这种方式就不能调用test了
向大家寻求一下问题所在。谢谢你们了。
解决方案 »
- 怎么响应COM组件的链接点事件
- 每秒刷新一次
- 如何实现压缩图片后上传到服务器?
- 如何使用javascript清空输入框的历史记录?
- 一个新手的问题
- 通过JS修改注册表信息 为横向打印
- function window.onload(){...}和 window.onload = somefunc; function somefunc(){...}有什么区别吗?
- 用jquery操作html,向<ul><li></li><li></li></ul>的第一li后面追加</ul><ul>的问题
- 关于web的数据库查询和弹出菜单,谢谢啦!大虾!
- 如何判断json格式为json
- 关于call(apply)继承法+原型继承法组合的一点小疑问
- jquery的选择器问题
var test=1;
alert(test);1.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> new document </title>
<meta name="generator" content="editplus" />
<meta name="author" content="" />
<meta name="keywords" content="" />
<meta name="description" content="" />
</head> <body>
<script type="text/javascript">
<!--
var a=document.createElement('script');
a.setAttribute("type","text/javascript");
a.src="1.js"; //1.js即外部JS
document.body.appendChild(a);
//-->
</script>
</body>
</html>
简单的说就是htm访问不到createElement('script')方式声明的变量。还是感谢您的回复
<html>
<head>
</head>
<body>
<script>
function f1()
{
var a=document.createElement('script');
a.setAttribute("type","text/javascript");
a.src="1.js";
document.body.appendChild(a);
alert(test);
}
f1();
</script>
</body>
</html>直接写的<script src="1.js"></script><script>alert(test);</script>
是没问题的
var test=1;
1.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> new document </title>
<meta name="generator" content="editplus" />
<meta name="author" content="" />
<meta name="keywords" content="" />
<meta name="description" content="" />
</head> <body>
<script type="text/javascript" defer>
<!--
var a=document.createElement('script');
a.setAttribute("type","text/javascript");
a.src="1.js"; //1.js即外部JS
document.body.appendChild(a);alert(test);
//-->
</script>
</body>
</html>
http://download.csdn.net/source/308916DHTML参考手册.rar —— DEFER Attribute | defer Property
http://download.csdn.net/source/308913样式表中文手册.chm
http://download.csdn.net/source/304124
像这种方式中,在调用函数f1的时候,变量test在函数的作用域内即当前产生的调用对象中是不存在的。而直接写,alert函数和test变量就都在全局作用域内是可以引用的。
<head> </head>
<body>
<script>
function f1()
{var a=document.createElement('script');
a.setAttribute("type","text/javascript");
a.src="1.js";
document.body.appendChild(a);
alert(test);
}
f1(); //变量test在函数的作用域内即当前产生的调用对象中是不存在的
</script>
</body>
</html>直接写的 <scriptsrc="1.js"> </script> <script>alert(test); </script>
是没问题的//而直接写,alert函数和test变量就都在全局作用域内是可以引用的。----变量作用域的问题,我第一次如此明白的理解了,注意到了。谢谢