2011-05-19 26 views
0

的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中的函數d汽車數量的文本追加爲這個彈出頁面的內容:

的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頁在新窗口browswer彈出,汽車數量始終爲0

正如你看到上面我所說的mycar.jsupdateCars()功能test.js但彈出窗口並未顯示更新的車號,如何在新彈出的窗口中獲取更新後的車(即2輛車,car1和car2)?

(我有「JS /」目錄下jQuery的1.5.1.js)

+0

完成了,還有一些回覆關於我的問題? – Leem 2011-05-19 12:28:42

回答

1

嘿,我貼在頁面的代碼,並顯示

number of cars: 2 

它似乎工作你只是引用腳本錯誤? 你有沒有試過Chrome或螢火蟲的調試器?

+0

megakorre是正確的,請參閱:http://jsbin.com/aguya4你的腳本是好的,你可能有一個腳本,包括工作不正常。 – 2011-05-19 14:34:25

相關問題