react 局部打印

import React, { Component } from 'react'
import {Row, Col,Button} from 'antd'

class Print extends React.Component{
    constructor (props) {
        super(props);
        this.state={
            printStyle:{}
        }
    }
    print(){
        this.setState({
            printStyle:{display:'none'}
        },function(){
            window.print();
            this.setState({
                printStyle:{}
            });
        });
    }
    render(){
        return(
            <div>
                <div id="print"></div>
                <div id="others" style={this.state.printStyle}>
	                <Button onClick={()=>this.print()}>打印</Button>
                </div>
            </div>
        )
    }
}
export default Print

id="print"的部分为打印出来的部分;id="others"为其他不用打印的部分。这里在点击打印时,将others设为隐藏(display:none),调用window.print()进行打印,打印完毕后,还原others的样式为显示。

猜你喜欢

转载自blog.csdn.net/sinat_38783046/article/details/88848985