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

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

这是我正在使用的代码

$('a.new-window').click(function(){ var recipe = window.open('','RecipeWindow','width=600,height=600'); $('#recipe1').clone().appendTo('#myprintrecipe'); var html = "Print Your Recipe
"; recipe.document.open(); recipe.document.write(html); recipe.document.close(); return false; });

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

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

 jQuery(function($) { $('a.new-window').click(function(){ var recipe = window.open('','RecipeWindow','width=600,height=600'); var html = 'Print Your Recipe
' + $('
').append($('#recipe1').clone()).html() + '
'; recipe.document.open(); recipe.document.write(html); recipe.document.close(); return false; }); });

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

希望这可以帮助。

问题出在你的字符串中。 把它改成这个:

 $('a.new-window').click(function(){ var recipe = window.open('','RecipeWindow','width=600,height=600'); $('#recipe1').clone().appendTo('#myprintrecipe'); var html = "Print Your Recipe
"; recipe.document.open(); recipe.document.write(html); recipe.document.close(); return false; });

问题是你在字符串中有一个“字符,它结束了字符串。脚本引擎感到困惑并抱怨错误信息。

改变:

 var html = "Print Your Recipe
";

至:

 var html = "Print Your Recipe
";

并记下双引号(“)