Javascript 新窗口的内容来自打开页上的模式内容

Javascript 新窗口的内容来自打开页上的模式内容,javascript,jquery,Javascript,Jquery,我认为我在这方面做得不太对,因为我对jquery非常陌生。 我有一个隐藏着3个食谱的页面。当点击配方a的链接时,它会在模式中打开。我希望能够打印出配方的内容。因此,我打开了一个新窗口(非模态),并尝试将配方写入其中(取决于选择的配方) 这是我正在使用的代码 $('a.new-window').click(function(){ var recipe = window.open('','RecipeWindow','width=600,height=600'); $

我认为我在这方面做得不太对,因为我对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;

     });
$('a.new-window')。单击(函数(){
var recipe=window.open(“”,'RecipeWindow','width=600,height=600');
$('#recipe1').clone().appendTo('#myprintrecipe');
var html=“打印您的配方”;
recipe.document.open();
配方。文档。编写(html);
recipe.document.close();
返回false;
});
但它返回一个错误。我认为这很接近,但不完全正确。错误是: 丢失的声明前
[Break on this error]var html=“Print…=”myprintrecipe“>”\n

问题出在您的字符串中。将其更改为:

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

     });
$('a.new-window')。单击(函数(){
var recipe=window.open(“”,'RecipeWindow','width=600,height=600');
$('#recipe1').clone().appendTo('#myprintrecipe');
var html=“打印您的配方”;
recipe.document.open();
配方。文档。编写(html);
recipe.document.close();
返回false;
});
问题是字符串中有一个“字符,它以字符串结尾。脚本引擎感到困惑,并抱怨出现错误消息。

更改:

var html = "<html><head><title>Print Your Recipe</title></head><body><div id="myprintrecipe"></div></body></html>";
var html=“打印您的配方”;
致:

var html=“打印您的配方”;

记下双引号(“)

我想你要找的是:

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;

    });
});
jQuery(函数($){
$('a.new-window')。单击(函数(){
var recipe=window.open(“”,'RecipeWindow','width=600,height=600');
var html='Print Your Recipe'+$('').append($('#recipe1').clone()).html()+'';
recipe.document.open();
配方。文档。编写(html);
recipe.document.close();
返回false;
});
});
Marius的回答部分正确-您的html字符串中确实存在javascript错误,但即使没有抛出错误,内容也不会被追加,因为您试图在新窗口中出现#myprintrecipe div之前追加配方


希望这有帮助。

谢谢tres!就是这样,弦乐的东西——DOH,菜鸟的错误,我不敢相信我错过了。第二个问题是我不知道什么时候做追加。。。谢谢好吧,我正在更仔细地看这个,但我并不真正理解它。(尽管它起作用了)。。。可以解释这部分吗:$('').append($('#recipe1').clone()).html()我不明白为什么/如何在这种情况下是选择器……div不是选择器,它是传递给jQuery的html。当这种情况发生时,jQuery将从中创建一个对象,您可以在链接中使用它。我对这段代码所做的是获取recipe对象的“外部”HTML,因为如果您只使用$(“#recipe1').HTML(),您将获得“#recipe1”内部的所有内容,而不是“#recipe1”本身,包括其HTML。如果您不需要在窗口中包含“#recipe1”,而只需要包含其HTML,那么只需转到$(“#recipe1').HTML()。jQuery文档在此处解释了有效参数:。请参阅:“$(…)jQuery函数:”。
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;

    });
});