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

时间:2011-05-19 12:23:11

标签: javascript jquery html jquery-ui javascript-events

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

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

功能MyTest 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;
        }
    };
}();

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

如上所述,我在 test.js 中调用了 mycar.js updateCars()功能但是弹出窗口没有显示更新的车号,如何在新弹出的窗口中获取更新的车(即2辆车,car1和car2)?

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

1 个答案:

答案 0 :(得分:1)

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

 number of cars: 2

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

相关问题