1

试图实现一个 Jquery 打印模板 (C# ASP.net),该模板从我的 Default.aspx 获取我的地图并将其附加到打印页面 (Print.htm) 以便我可以编辑 HTML。至今...

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"</script>

  <script type="text/javascript">

  $(document).ready(function() {
  $("#Printbtn").click(function() {  

  var mapObj = $("MapCell_Map1");
  var mapHtmlStr = mapObj.html();

         mapHtmlStr = mapHtmlStr.replace(/CURSOR: crosshair; /g, "");
         mapHtmlStr = mapHtmlStr.replace("Maps['Map1'].pendingTiles.remove",          "return;");
         mapHtmlStr = mapHtmlStr.replace("Maps['Map1'].pendingTiles.remove", "return;");
         mapHtmlStr = mapHtmlStr.replace("Maps['Map1'].pendingTiles.remove", "return;");
         mapHtmlStr = mapHtmlStr.replace("Maps['Map1'].pendingTiles.remove", "return;");
         mapHtmlStr = mapHtmlStr.replace("Maps['Map1'].keyFocus=true;", "");


        $.ajax({url:"print.htm", context: document.body, 
        success: function(response){


        var printDoc = $(response);
        printDoc.find("#mapPanel").html(mapHtmlStr);
        var pwin = window.open("Print.htm");
        var pdoc = window.document.open();
        pdoc.write(printDoc.html());
        pdoc.close();

        });

        return false;

  });
  });     
</script>

不会触发,只是在单击按钮后发回...

<asp:Button runat="server" id="Printbtn" Text="Print" Forecolor="white"/>

打印.htm 页...

html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>


</head>
<body>
<div id="MapPanel">

</div>
</body>
</html>
4

1 回答 1

1

如果您要添加的 Html 是静态的,您可以将单独的 Html 文件加载到内存中,然后在将内容写入新窗口之前将动态 Html 添加到隐藏的 div

jQuery 示例(未经测试):

function printmap() {
    var mapObj = $("#Map1");
    var mapHtmlStr = mapObj.html();
    // snip - do string replacements as shown in question
    $.ajax({url:"print.html", context: document.body, 
        success: function(response){
            var printDoc = $(response);
            printDoc.find("#mapPanel").html(mapHtmlStr);
            var pwin = window.open("#");
            var pdoc = window.document.open();
            pdoc.write(printDoc.html());
            pdoc.close();
    });
}

在 Javascript 中加载 Html 文件的更多非 jQuery 示例显示在这篇文章中:

于 2013-02-20T14:21:15.203 回答