简体   繁体   English

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

[英]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:函数MyTestmycar.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.jsupdateCars()函数,但弹出窗口没有显示更新的汽车编号,我怎样才能在新的弹出窗口?

(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.

相关问题 为什么我的div无法填满100%的窗口? - Why can't I get my div to fill up 100% of my window? 我如何与一个弹出窗口来回通信 - How do I communicate back and forth with a popped up window 我怎么知道数组元素何时被弹出() - How can I tell when an array element has been .popped() 打开/关闭“弹出”窗口时出现问题 - Problems opening/closing a 'popped-up' window 如何使Cortana使用新的或更新的语音命令定义文件? - How can I get Cortana to use my new or updated Voice Command Definition files? 我的机器人如何检查 js 警报是否弹出(并且可见)? - How can my bot check if a js alert has popped up (and is visible)? 为什么我无法访问使用“new Array”构造函数创建的二维数组的 [0][0] position? - Why can't I access the [0][0] position of my 2 dimensional array created with 'new Array' constructor? 如何让jQuery引用一个新弹出的窗口? - How to have jQuery refer to a newly popped up window? 为什么我的HTML按钮刷新页面?/为什么我不能将新的HTML DOM元素插入页面中? - Why do my HTML buttons refresh the page?/Why can't I get my new HTML DOM Element to insert into my page 如果我只对数组 1 应用排序并将 array2 创建为新数组,有人可以解释为什么 array2 会被排序吗? 这是 javascript - Can someone please explain why is array2 get sorted if I only applied sorting on Array 1 and created array2 as a NEW array? This is javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM