代码之家  ›  专栏  ›  技术社区  ›  Kung Fu Ninja

从模式窗口打印的jquery

  •  2
  • Kung Fu Ninja  · 技术社区  · 16 年前

     // Create a jquery plugin that prints the given element.
    jQuery.fn.print = function(){
    // NOTE: We are trimming the jQuery collection down to the
    // first element in the collection.
    if (this.size() > 1){
        this.eq( 0 ).print();
        return;
    } else if (!this.size()){
        return;
    }
    
    // ASSERT: At this point, we know that the current jQuery
    // collection (as defined by THIS), contains only one
    // printable element.
    
    // Create a random name for the print frame.
    var strFrameName = ("printer-" + (new Date()).getTime());
    
    // Create an iFrame with the new name.
    var jFrame = $( "<iframe name='" + strFrameName + "'>" );
    
    // Hide the frame (sort of) and attach to the body.
    jFrame
        .css( "width", "1px" )
        .css( "height", "1px" )
        .css( "position", "absolute" )
        .css( "left", "-9999px" )
        .appendTo( $( "body:first" ) )
    ;
    
    // Get a FRAMES reference to the new frame.
    var objFrame = window.frames[ strFrameName ];
    
    // Get a reference to the DOM in the new frame.
    var objDoc = objFrame.document;
    
    // Write the HTML for the document. In this, we will
    // write out the HTML of the current element.
    objDoc.open();
    objDoc.write( "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">" );
    objDoc.write( "<html>" );
    objDoc.write( "<body>" );
    objDoc.write( "<head>" );
    objDoc.write( "<title>" );
    objDoc.write( document.title );
    objDoc.write( "</title>" );
    objDoc.write( "<div id='viewNotes'></div>" );
    objDoc.write( "</head>" );
    //objDoc.write( this.html() );
    objDoc.write( "</body>" );
    objDoc.write( "</html>" );
    //objDoc.close();
    
    formData = 'cmid=C93F52DCE21ABBD5';
    $.ajax({
        url: 'ajax/ajax_get_notes.cfm',
        type: 'GET',
        data: formData,
        cache: false,
        success: function(result) {
            $('#viewNotes').html(result);
        },
        error: function(xmlHttpRequest, status, err) {
            $('#viewNotes').html('error:'+err);
        }
    });     
    
    // Print the document.
    objFrame.focus();
    //objFrame.print();
    
    // Have the frame remove itself in about a minute so that
    // we don't build up too many of these frames.
    setTimeout(
        function(){
            jFrame.remove();
        },
        (60 * 1000)
        );
    
    alert('i am here');
    }
    

    我做错什么了?有没有更好的方法来实现这一点?

    2 回复  |  直到 16 年前
        1
  •  2
  •   Dave    16 年前

    我发现有几件事不对。

    首先,不创建iFrame,因为没有结束标记。其次,iFrame从未添加到页面中。第三,jFrame CSS没有正确声明(应该是'jFrame')。第四,html标记的顺序不对。

    我想你可以这样做:

    var strFrameName = ("printer-" + (new Date()).getTime());
    
    // Create an iFrame with the new name.
    var jFrame = $( "<iframe name='" + strFrameName + "'><div id='viewNotes'></div></iframe>" );
    
    // Get a FRAMES reference to the new frame.
    var objFrame = window.frames[ strFrameName ];
    
    // Get a reference to the DOM in the new frame.
    var objDoc = objFrame.document;
    
    // Write the HTML for the document. In this, we will
    // write out the HTML of the current element.
    objDoc.open();
    objDoc.write( "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"  \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">" );
    objDoc.write( "<html>" );
    objDoc.write( "<head>" );
    objDoc.write( "<title>" );
    objDoc.write( document.title );
    objDoc.write( "</title>" );
    objDoc.write( "</head>" );
    objDoc.write( "<body>" );
    objDoc.write( jframe );
    objDoc.write( "</body>" );
    objDoc.write( "</html>" );
    //objDoc.close();
    
    formData = 'cmid=C93F52DCE21ABBD5';
    $.ajax({
       url: 'ajax/ajax_get_notes.cfm',
       type: 'GET',
       data: formData,
       cache: false,
       success: function(result) {
           $('#viewNotes').html(result);
       },
       error: function(xmlHttpRequest, status, err) {
           $('#viewNotes').html('error:'+err);
       }
    });     
    //print
    
        2
  •  0
  •   Kung Fu Ninja    16 年前

    我最终实现了不同的目标。我创建了另一个名为printNotes.cfm的页面,它类似于notes模式窗口。我在模式窗口的“可打印版本”按钮上附加了一个window.open事件。单击后,将启动printNotes.cfm,此页面将通过ajax加载注释。在printNotes.cfm中,有一个打印机按钮,单击后会触发window.print()事件。

    它就像一个符咒。

    谢谢