New window with content from modal content on opener page

前端 未结 3 1748
野的像风
野的像风 2020-12-10 07:45

i think i am not going about this quite right, being very new to jquery. i have a page with 3 recipes on it hidden. when a link to recipe A is clicked it opens in a modal.

相关标签:
3条回答
  • 2020-12-10 07:49

    I think what you are looking for is the following:

    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;
    
        });
    });
    

    Marius had a partially correct answer - you did have a javascript error in your html string, but the content wouldn't have been appended anyways even if the error didn't get thrown because you were trying to append the recipe before the #myprintrecipe div was even in the new window.

    Hope this helps.

    0 讨论(0)
  • 2020-12-10 07:54

    The problem is in your string. Change it to this:

    $('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;
    
         });
    

    The problem is that you had a " character inside the string, which ends the string. The script engine got confused and complained with the error message.

    0 讨论(0)
  • 2020-12-10 08:01

    change :

    var html = "<html><head><title>Print Your Recipe</title></head><body><div id="myprintrecipe"></div></body></html>";
    

    to:

    var html = "<html><head><title>Print Your Recipe</title></head><body><div id='myprintrecipe'></div></body></html>";
    

    and take notes on the double quotes (")

    0 讨论(0)
提交回复
热议问题