为什么获取不到js全局变量--高分跪求大神帮我解决

qianfoyuan 2011-09-30 10:13:02
我要获取的确是 A.js 中全局变量 b,各位可以将下面代码拷下去,分成两个文件, A.js test.html.然后点击 test.html里面会生成一个 iframe 框架,但我要获取 父窗体中 b 变量,但 IE Firefox 提示 b 未定义.跪求大神帮我解决,解决问题者给高分.万分感谢.

JS A页面:
function A()
{
this.x = 6;
this.y = 7;
}
var b = 5;


HTML 页面:
<html>
<head>
<script type="text/javascript" src="./A.js"></script>
<script type="text/javascript">
var x = 5;
var a = new A();
</script>
</head>
<body>
<input id = "readme" type = "text" value="helloworld"></input><br>
<script>
window.onload=function(){
var str="<div><iframe width=400 height=300 frameborder='yes' scrolling='no' style='position:absolute'></iframe></div>";
document.write(str);

var mystr = "";
mystr += "<html>";
mystr += " <head>";
mystr += " <script type='text/javascr" + "ipt' defer>";
mystr += " function func(){";
mystr += " var x=parent.b;document.getElementsByTagName('input')[0].value=x;"; mystr += " }";
mystr += " </" + "scr" + "ipt>";
mystr += " </head>";
mystr += " <body>";
mystr += " <input type='button' value='' onclick='func();'></input>";
mystr += " </body>";
mystr += "</head>";

window.frames[0].document.write(mystr);
window.frames[0].document.close();}
</script>
<p>一些老的浏览器不支持 iframe。</p>
<p>如果得不到支持,iframe 是不可见的。</p>
</body>
</html>



为什么不能获取不到 b 全局变量?

...全文
482 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
峭沙 2011-09-30
  • 打赏
  • 举报
回复
呵呵,document.write要慎用
qianfoyuan 2011-09-30
  • 打赏
  • 举报
回复
非常感谢 axiheyhey 谢谢你了.
qianfoyuan 2011-09-30
  • 打赏
  • 举报
回复
好谢谢三楼,非常谢三楼,我明白了,把
var str="<div><iframe width=400 height=300 frameborder='yes' scrolling='no' style='position:absolute'></iframe></div>";
document.write(str);
放在window.onload外可以避免函数调用导致window销毁.同时也感谢二楼
峭沙 2011-09-30
  • 打赏
  • 举报
回复
因为document.write(str);这句话,你把父窗口的所有元素,包括js全部清除掉了。。
<html>
<head>
<script type="text/javascript">
function A(){
this.x = 6;
this.y = 7;
alert(this.x);
}
c = 1;
var b = 5;
var x = 5;
//var a = new A();
</script>
</head>
<body>
<input id = "readme" type = "text" value="helloworld"></input><br>
<script>
window.onload=function(){
//var str="<iframe width=400 height=300 frameborder='yes' scrolling='no' style='position:absolute'></iframe>";
//document.write(str);
var iframe = document.createElement('iframe');
iframe.width = 400;
iframe.height = 300;
iframe.frameborder = 'yes';
iframe.scrolling='no';
iframe.style.position = 'absolute';
document.body.appendChild(iframe);


var mystr = "";
mystr += "<html>";
mystr += " <head>";
mystr += " <script type='text/javascr" + "ipt'>";
mystr += " function func(){";
mystr += " var x=parent.b;document.getElementsByTagName('input')[0].value=x;";
mystr += " }";
mystr += " </" + "scr" + "ipt>";
mystr += " </head>";
mystr += " <body>";
mystr += " <input type='button' value='' onclick='func();'></input>";
mystr += " </body>";
mystr += "</head></html>";
window.frames[0].document.open();
window.frames[0].document.write(mystr);
window.frames[0].document.close();
}
</script>
<p>一些老的浏览器不支持 iframe。</p>
<p>如果得不到支持,iframe 是不可见的。</p>
</body>
</html>
晨晨 2011-09-30
  • 打赏
  • 举报
回复
你在JS文件中,不要将变量放在<script language=javascript></script>之间,即不需要这个标签,就可以了
Crazywa 2011-09-30
  • 打赏
  • 举报
回复
亲,你defer了...
VC6下安装与配置 Wikipedia,自由的百科全书 本手册适应 OpenCV 1.0 安装。 目录 [隐藏] 1 安装Visual C++ 6.0 2 安装OpenCV 3 配置Windows环境变量 4 配置Visual C++ 6.0 4.1 全局设置 4.2 项目设置 [编辑]安装Visual C++ 6.0 略。 [编辑]安装OpenCV 从http://www.opencv.org.cn/ 下载OpenCV安装程序。假如要将OpenCV安装到C:\\Program Files\\OpenCV。(下面附图为OpenCV 1.0rc1的安装界面,OpenCV 1.0安装界面与此基本一致。)在安装时选择\"将\\OpenCV\\bin加入系统变量\"(Add\\OpenCV\\bin to the systerm PATH)。 [编辑]配置Windows环境变量 检查C:\\Program Files\\OpenCV\\bin是否已经被加入到环境变量PATH,如果没有,请加入。 [编辑]配置Visual C++ 6.0 [编辑]全局设置 菜单Tools->Options->Directories:先设置lib路径,选择Library files,在下方填入路径: C:\\Program Files\\OpenCV\\lib 然后选择include files,在下方填入路径: C:\\Program Files\\OpenCV\\cxcore\\include C:\\Program Files\\OpenCV\\cv\\include C:\\Program Files\\OpenCV\\cvaux\\include C:\\Program Files\\OpenCV\\ml\\include C:\\Program Files\\OpenCV\\otherlibs\\highgui C:\\Program Files\\OpenCV\\otherlibs\\cvcam\\include [编辑]项目设置 每创建一个将要使用OpenCV的VC Project,都需要给它指定需要的lib。菜单:Project->Settings,然后将Setting for选为All Configurations,然后选择右边的link标签,在Object/library modules附加上 cxcore.lib cv.lib ml.lib cvaux.lib highgui.lib cvcam.lib 如果你不需要这么多lib,你可以只添加你需要的lib。

87,996

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧