【问题标题】:New window with content from modal content on opener page新窗口,内容来自开启器页面上的模态内容
【发布时间】:2014-11-13 03:43:39
【问题描述】:

我认为我对这件事不太正确,对 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;

     });

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

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

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

    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 有一个部分正确的答案 - 您的 html 字符串中确实存在 javascript 错误,但即使没有抛出错误,内容也不会被追加,因为您试图在#myprintrecipe div 甚至在新窗口中。

    希望这会有所帮助。

    【讨论】:

    • 感谢特雷斯!就是这样,字符串的东西——DOH 菜鸟的错误,我不敢相信我错过了。第二个问题是我想不通,什么时候做追加...谢谢!!!
    • 好吧,我在看这个更仔细,并没有真正理解它。 (虽然很感激它有效)......是否可以解释这部分: $('
      ').append($('#recipe1').clone()).html() 我不明白为什么/
      在这种情况下如何成为选择器...
    • div 不是选择器,它是传递给 jQuery 的 html。发生这种情况时,jQuery 会从中创建一个可以在链接中使用的对象。我用那段代码做的是抓取食谱对象的“外部”HTML,因为如果你只是去$('#recipe1').html(),你会得到'#recipe1'里面的所有东西,但是不是“#recipe1”本身,包括它的 HTML。如果您不需要在窗口中包含“#recipe1”,而只需要包含它的 HTML,那么只需使用 $('#recipe1').html()。 jQuery 文档在这里解释了有效的参数:docs.jquery.com/Core。请参阅:“$(...) jQuery 函数:”。
    【解决方案2】:

    问题出在您的字符串中。改成这样:

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

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

    【讨论】:

      【解决方案3】:

      改变:

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

      到:

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

      并在双引号(")上做笔记

      【讨论】:

        猜你喜欢
        相关资源
        最近更新 更多
        热门标签