[英]Why I can not get the updated "cars" array in my new popped up window
My index.html has a button( id="my-btn"
):我的index.html有一个按钮(
id="my-btn"
):
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>My INDEX PAGE</title>
</head>
<body>
<br><input type="button" id="my-btn" value="OPEN NEW WINDOW"/>
<script src="js/my.js"></script>
</body>
</html>
above page include my.js which handles button click event, when button clicked, a new browser window will be opened with a new page( test.html )上面的页面包含处理按钮单击事件的my.js ,当单击按钮时,将打开一个新的浏览器窗口,其中包含一个新页面( test.html )
my.js:我的.js:
$('#my-btn').click(function(){
window.open('test.html', 'testwindow');
});
The new page ( test.html ) opened/popped up in new browser window: test.html:新页面 ( test.html ) 在新浏览器窗口中打开/弹出: test.html:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>TEST</title>
</head>
<body>
<div id="cars"></div>
<script src="js/jquery-1.5.1.js"></script>
<script src="js/mycar.js"></script>
<script src="js/test.js"></script>
</body>
</html>
In the new page, test.js is included which will call functions in MyTest
and append the text of number of cars as the content of this pop-up page:在新页面中,包含了test.js ,它将调用
MyTest
中的函数并附加汽车数量的文本作为此弹出页面的内容:
js/test.js js/test.js
$(document).ready(function(){
MyTest.updateCars(); //MyTest is a function defined in mycar.js
var cars = MyTest.getCars();
$("#cars").append("<strong>number of cars: "+cars.length+"</strong>");
});
Function MyTest
is defined in mycar.js:函数
MyTest
在mycar.js 中定义:
js/mycar.js js/mycar.js
var MyTest = function(){
var cars=[];
var updateCars=function(){
cars.push('car 1', 'car 2');
};
return{
updateCars: function(){
updateCars();
},
getCars: function(){
console.log(cars.length);
return cars;
}
};
}();
But, when the test.html page popped up in a new browswer window, the number of cars is always 0.但是,当test.html页面在新的浏览器窗口中弹出时,汽车的数量始终为 0。
As you saw above I have called updateCars()
function of mycar.js in test.js but the pop up window does not show the updated cars number , how can I get the updated cars(that's 2 cars, car1 and car2) in the new popped up window?正如您在上面看到的,我在test.js中调用了mycar.js的
updateCars()
函数,但弹出窗口没有显示更新的汽车编号,我怎样才能在新的弹出窗口?
(I have jquery-1.5.1.js under "js/" directory) (我在“js/”目录下有 jquery-1.5.1.js)
hey i pasted your code in a page and it displayed嘿,我将您的代码粘贴到页面中并显示
number of cars: 2
it seems to work are you just referencing the script wrong?它似乎工作你只是引用脚本错误吗? have you tried the debugger on chrome or firebug?
您是否尝试过 chrome 或 firebug 上的调试器?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.