Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[js] 第137天 用js实现页面局部打印和预览原理是什么呢?同时在IE上有什么不同? #1134

Open
haizhilin2013 opened this issue Aug 30, 2019 · 6 comments
Labels
js JavaScript

Comments

@haizhilin2013
Copy link
Collaborator

第137天 用js实现页面局部打印和预览原理是什么呢?同时在IE上有什么不同?

@haizhilin2013 haizhilin2013 added the js JavaScript label Aug 30, 2019
@bowencool
Copy link

bowencool commented Aug 31, 2019

#print {
  display: none !important;
}
@media print {
  body > * {
    display: none;
  }
  #print {
    display: block!important;
  }
}

以React为例封装一个PrintContainer

import ReactDOM from 'react-dom'
const div = document.createElement('div')
div.id = 'print'
document.body.appendChild(div)

export default function PrintContainer (props) {
  return ReactDOM.createPortal(props.children, div)
}

@xiaotianxia
Copy link

@media print {
    *:not(.somedom) {
         display: none;
    }
}

这样吗

@bowencool
Copy link

@media print {
    *:not(.somedom) {
         display: none;
    }
}

这样吗

这样写的话.somedom的子元素也不见了吧?😰

@xiaotianxia
Copy link

@media print {
    *:not(.somedom) {
         display: none;
    }
}

这样吗

这样写的话.somedom的子元素也不见了吧?😰

是哦 是我草率了:grin:

@censek
Copy link

censek commented Feb 19, 2020

<h1>这块内容不需要打印1</h1>
<!--startprint-->
<div class="content">
    这里是需要打印的内容
    .....
</div>
<!--endprint-->
<h1>这块内容不需要打印2</h1>
<button onclick="doPrint()">打印按钮</button>
function doPrint() {
    bdhtml = window.document.body.innerHTML;
    sprnstr = "<!--startprint-->";
    eprnstr = "<!--endprint-->";
    prnhtml = bdhtml.substr(bdhtml.indexOf(sprnstr) + 17);
    prnhtml = prnhtml.substring(0, prnhtml.indexOf(eprnstr));
    window.document.body.innerHTML = prnhtml;
    window.print();
}

@xiaoqiangz
Copy link

bdhtml=window.document.body.innerHTML;
sprnstr="";
eprnstr="";
prnhtml=bdhtml.substr(bdhtml.indexOf(sprnstr)+17);
prnhtml=prnhtml.substring(0,prnhtml.indexOf(eprnstr));
window.document.body.innerHTML=prnhtml;
window.print();
window.document.body.innerHTML = bdhtml;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
js JavaScript
Projects
None yet
Development

No branches or pull requests

5 participants