javascript - 打印 div 在 chrome 中不起作用并且没有将样式作为链接

标签 javascript jquery html css google-chrome

我正在用这个 http://www.bennadel.com/blog/1591-ask-ben-print-part-of-a-web-page-with-jquery.htm

使用这个jquery打印一个div,但是在chrome中不工作,而且它需要在同一页面中使用内部样式,不能使用外部css。

<html>
<head>
// code
        <link rel="stylesheet" rel="stylesheet" media="screen" href="style.css">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootssstrap-theme.min.css">
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
        <script src="jquery.print.js.js"></script>

       <script>
        $(document).ready(function(){ 
            // Hook up the print link.
            $("#print_div").click(function(){
                alert("asd");
                // Print the DIV.
                    $( ".printable" ).print();
                    // Cancel click event.
                    return( false );
                });
            });

        </script>
      <head>  
      <body>

// code
<div class="container text-center">
    <a href="#" class="btn btn-primary btn-lg print" id="print_div" >Print</a>
</div>
   <div class="printable">
       // section that need to be printed
       // images, css form 
       // code
</div>
<body>
    </html>

任何更好的建议,我们可以将其整合 https://github.com/jasonday/printThis

if (opt.importCSS) $("link[rel=stylesheet]").each(function() {
                var href = $(this).attr("href");
                if (href) {
                    var media = $(this).attr("media") || "all";
                    $doc.find("head").append("<link type='text/css' rel='stylesheet' href='" + href + "' media='" + media + "'>")
                }
            });

最佳答案

编辑、更新

“使用那个 Bootstrap ,它与它的 cdn 相同。” 尝试 http://jsfiddle.net/dLk2kwx9/7 . maxcdn 的 https 协议(protocol)请求返回了 403 FORBIDDEN 错误。尝试在 linkscript 元素 href 中用 http 协议(protocol)替换 https 协议(protocol)code>src 属性,分别。应用的样式应反射(reflect)在 .container.btn 元素中。


请注意,.printable 类没有出现在 OP 的 html 中?不确定要在 printable 中应用哪些特定样式? , hreflink 元素在问题结束时引用?在 chrome 37 上试过

尝试

html

<div class="container text-center">
    <a href="#" class="btn btn-primary btn-lg print" id="print_div">Print</a>
</div>
<!-- added `printable` class to `div` to be be printed -->
<div class="printable">
       stuff to print
</div>

CSS

/* `printable` styles to be applied before print, removed after print */
.printstyle {    
    font-size : 36px;
}

js

$(function() {
    $(".print").on("click", function(e) {
      e.preventDefault();
        $("body *:not(.printable)").hide(function() {
          $(".printable").addClass("printstyle");
            // $("head").append("<link>", {
            // "href":"url"
            // "rel":"stylesheet"
            // });
            window.print()
        }).promise().done(function(el) {
          $(".printable").removeClass("printstyle");  
          el.show()
        });
    })
})

jsfiddle http://jsfiddle.net/guest271314/dLk2kwx9/

关于javascript - 打印 div 在 chrome 中不起作用并且没有将样式作为链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26634675/

相关文章:

javascript - 使用 HTML 和 JavaScript 的 WebGL 全屏按钮?

javascript - 如何使用 JavaScript 正则表达式向文本添加标记

javascript - 带有两个按钮的 ListView 行设置属性div

javascript - 使用正则表达式的名称验证

javascript - 如何在 django 的 FormData 中附加更多数据?

javascript - 父容器不尊重子容器的高度

jquery - 重定向时能够看到在所有页面上打开的对话框

javascript - 我无法让我的待办事项列表正常工作

javascript - 从 json 数据中获取唯一值

javascript - JavaScript/Node 中如何调用它?