在弹出窗口中编写js脚本

时间:2015-02-08 02:11:55

标签: javascript html

我正在尝试将js脚本添加到我打开的弹出窗口中但是却无法这样做。

这是我的代码:

function openGame() {
    gameWindow = window.open("", "gameWindow", "top=500, left=500, height=988, width=1263");
    gameWindow.document.write('<html><head>')
    gameWindow.document.write('<link rel=\"stylesheet\" href=\"cssbutton.css\">')
    gameWindow.document.write('<link rel=\"stylesheet\" type=\"text/css\" href=\"sbg_style.css\">')
}
// at this point I would like to add the following script
$(document).ready(function(){
    $("#checker0").click(function(){
        $("div").animate({left: '250px', top:'250px'});
    });
});

怎么做?

1 个答案:

答案 0 :(得分:5)

这是您的模板:

var gameWindow = window.open("", 
    "gameWindow", 
    "top=500, left=500, height=988, width=1263");    // external/pop-up window
gameWindow.document.write('<div id="checker">+++++++++</div>');   // example html element
// adding a listener to the element on a external/pop-up window
// there is extended notation for jQuery selector
$("#checker", gameWindow.document).click(function(e){ // listener of element on a external window
  // example of manipulation on a "#checker" div
  $(e.target).text("---------"); // there you can do anything with elements on external window
});

DEMO:http://jsbin.com/qotaxoguvi/1/