簡體   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