无法打印弹出对话框

时间:2016-08-23 19:59:16

标签: javascript jquery

我需要打印一个弹出对话框。我已经尝试了几次,但我只能通过弹出对话框获取整个窗口,但只需要打印对话框。

我找到了一个可以打印对话框的解决方案,但现在它没有显示打印时在文本框中输入的值。

请帮我查看下面的代码,或者提供很好的参考资料,用文本框内的值打印div值。

这是我编写的用于打印弹出对话框的代码。

<script>
    function PrintContent()
    {
        var DocumentContainer = document.getElementById('scorecard');
        var WindowObject = window.open("", "PrintWindow",
        "width=750,height=650,top=50,left=50,toolbars=no,scrollbars=yes,status=no,resizable=yes");
        WindowObject.document.writeln('<!DOCTYPE html>');
        WindowObject.document.writeln('<html><head><title></title>');
        var str = "<style type='text/css' media='all'>";
        str = str + "MyStyles { text-align: center; }";
        str = str + "</style>";
        WindowObject.document.writeln(str);
        WindowObject.document.writeln('</head><body>');
        WindowObject.document.writeln(DocumentContainer.innerHTML);
        WindowObject.document.writeln('</body></html>');
        WindowObject.document.close();
        WindowObject.focus();
        WindowObject.print();
        WindowObject.close();
    }
</script>

2 个答案:

答案 0 :(得分:0)

弹出模式

HTML

<!-- Trigger/Open The Modal -->
<button id="myBtn">Open Modal</button>

<!-- The Modal -->
<div id="myModal" class="modal">

  <!-- Modal content -->
  <div class="modal-content">
    <span class="close">x</span>
    <p>Some text in the Modal..</p>
  </div>

</div>

CSS

/* The Modal (background) */
.modal {
    display: none; /* Hidden by default */
    position: fixed; /* Stay in place */
    z-index: 1; /* Sit on top */
    left: 0;
    top: 0;
    width: 100%; /* Full width */
    height: 100%; /* Full height */
    overflow: auto; /* Enable scroll if needed */
    background-color: rgb(0,0,0); /* Fallback color */
    background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}

/* Modal Content/Box */
.modal-content {
    background-color: #fefefe;
    margin: 15% auto; /* 15% from the top and centered */
    padding: 20px;
    border: 1px solid #888;
    width: 80%; /* Could be more or less, depending on screen size */
}

/* The Close Button */
.close {
    color: #aaa;
    float: right;
    font-size: 28px;
    font-weight: bold;
}

.close:hover,
.close:focus {
    color: black;
    text-decoration: none;
    cursor: pointer;
}

JS

// Get the modal
var modal = document.getElementById('myModal');

// Get the button that opens the modal
var btn = document.getElementById("myBtn");

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];

// When the user clicks on the button, open the modal 
btn.onclick = function() {
    modal.style.display = "block";
}

// When the user clicks on <span> (x), close the modal
span.onclick = function() {
    modal.style.display = "none";
}

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
    if (event.target == modal) {
        modal.style.display = "none";
    }
}

答案 1 :(得分:-1)

如果您正在寻找,可以使用bootstrap模式框。这是代码,只需将其粘贴到您的html文件中,看看它是否是您想要的。

&#13;
&#13;
<html>
    <head>
    <!-- Bootstrap Core CSS -->
    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    </head>
    
    <body>
        
        <div class="container">

  <!-- Modal -->
  <div class="modal fade" id="myModal" role="dialog">
    <div class="modal-dialog">

      <!-- Modal content-->
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h4 class="modal-title" style="color:#333;">Buy Membership</h4>
        </div>
        <div class="modal-body">
            <p style="color:#777;">You are not a premium member of Pagpry and thus you could use only limited images and features. Buy membership for more features and tools.</p>
           <button type="button" class="btn btn-default"> <a href="#">Click Here to Know about Membership Plans.</a></button>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        </div>
      </div>

    </div>
        
          <script type="text/javascript" src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
    <script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
      
    <script>
        $(document).ready(function(){
            alert('works');
            $('#myModal').modal();
            alert('works');
        });
        
        
    </script>
    </body>
</html>
&#13;
&#13;
&#13;