繁体   English   中英

为什么我无法在新的弹出窗口中获取更新的“汽车”数组

[英]Why I can not get the updated "cars" array in my new popped up window

我的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>

上面的页面包含处理按钮单击事件的my.js ,当单击按钮时,将打开一个新的浏览器窗口,其中包含一个新页面( test.html

我的.js:

$('#my-btn').click(function(){  
  window.open('test.html', 'testwindow');
});

新页面 ( 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>

在新页面中,包含了test.js ,它将调用MyTest中的函数并附加汽车数量的文本作为此弹出页面的内容:

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>");
});

函数MyTestmycar.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;
        }
    };
}();

但是,当test.html页面在新的浏览器窗口中弹出时,汽车的数量始终为 0。

正如您在上面看到的,我在test.js中调用了mycar.jsupdateCars()函数,但弹出窗口没有显示更新的汽车编号,我怎样才能在新的弹出窗口?

(我在“js/”目录下有 jquery-1.5.1.js)

嘿,我将您的代码粘贴到页面中并显示

 number of cars: 2

它似乎工作你只是引用脚本错误吗? 您是否尝试过 chrome 或 firebug 上的调试器?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM