新窗口,内容来自开启页面上的模态内容

6 javascript jquery

我想我不会这么做,对jquery来说很新.我有一个隐藏着3个食谱的页面.当点击配方A的链接时,它会以模态打开.我希望能够只打印食谱的内容.所以我打开一个新窗口(非模态)并尝试将配方写入其中(取决于选择的配方)

这是我正在使用的代码

     $('a.new-window').click(function(){

    var recipe =  window.open('','RecipeWindow','width=600,height=600');
     $('#recipe1').clone().appendTo('#myprintrecipe');
    var html = "<html><head><title>Print Your Recipe</title></head><body><div id="myprintrecipe"></div></body></html>";
recipe.document.open();
recipe.document.write(html);
recipe.document.close();

      return false;

     });
Run Code Online (Sandbox Code Playgroud)

但它返回一个错误.我认为它很接近但不太正确.错误是:缺失; 在声明之前[打破此错误] var html ="Print ... ="myprintrecipe">"; \n

Tre*_*res 23

我认为您正在寻找的是以下内容:

jQuery(function($) {
    $('a.new-window').click(function(){

    var recipe =  window.open('','RecipeWindow','width=600,height=600');
    var html = '<html><head><title>Print Your Recipe</title></head><body><div id="myprintrecipe">' + $('<div />').append($('#recipe1').clone()).html() + '</div></body></html>';
    recipe.document.open();
    recipe.document.write(html);
    recipe.document.close();

    return false;

    });
});
Run Code Online (Sandbox Code Playgroud)

Marius有一个部分正确的答案 - 你的html字符串确实有一个javascript错误,但是即使错误没有被抛出也不会被追加,因为你试图在#myprintrecipe div之前附加食谱甚至在新的窗口.

希望这可以帮助.