最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - print a modal window not the rest of the page - Stack Overflow

programmeradmin1浏览0评论

I have seen some answers to this but I think I am actually having a CSS issue.

Here is a sample fragment of my HTML:

<id=container> children <id="modal">children</modal></container>

If I pop open the modal there is a print button. When I click it I use jQuery to apply a .noprint to #container and .print to #modal. But the .print to modal is disregarded, no matter what selector I apply I can't get the modal content to appear. What's worse, I cant use firebug, it is not showing me what print styles are being applied. I have tried #container #modal.print {display: block; } etc...

thanks!

I have seen some answers to this but I think I am actually having a CSS issue.

Here is a sample fragment of my HTML:

<id=container> children <id="modal">children</modal></container>

If I pop open the modal there is a print button. When I click it I use jQuery to apply a .noprint to #container and .print to #modal. But the .print to modal is disregarded, no matter what selector I apply I can't get the modal content to appear. What's worse, I cant use firebug, it is not showing me what print styles are being applied. I have tried #container #modal.print {display: block; } etc...

thanks!

Share Improve this question edited Aug 11, 2014 at 15:45 AlexB 7,41612 gold badges60 silver badges76 bronze badges asked Aug 8, 2009 at 0:01 lizwlizw 1
  • Is your #modal window inside the #container div? By the looks of your last line of code, it shows that this is the case. Could you post your print CSS file and the relevant HTML? – Kevin Lamping Commented Aug 8, 2009 at 0:50
Add a comment  | 

2 Answers 2

Reset to default 10

From the sound of it, your modal element is inside of the container. I'm guessing you have markup like this.

<div id="container">
    <span>children</span>
    <p>more children>
    <div id="modal">
        Modal text!
    </div>
</div>

And CSS like this...

.noprint { display: none;}
.print {display: block;}

Which you use JavaScript to apply, like this:

<div id="container" class="noprint">
    <span>children</span>
    <p>more children>
    <div id="modal" class="print">
        Modal text!
    </div>
</div>

In these examples, the #modal element is inside of a hidden container. The CSS spec dictates that if a parent element is hidden, all children will be hidden also, even if they are set to display: block (or any other valid display value) or are absolutely positioned.

What you should do if you want to hide everything on the page except for your modal window is get your markup to be structured something like this:

<div id="modal">
    Modal Text!
</div>
<div id="container">
    <span>children</span>
    <p>more children>
    <div id="modal">
        Modal text!
    </div>
</div>

If you have no control over where the modal element is rendered, you can use jQuery to re-position the modal element. Keep in mind that this may impact your modal window's layout:

// Moves the element outside of the hidden container
$("#container").before($("#modal"));

There is another way to print modal window contents. Add this function to modal window

Print contents of ModalPanel

I have tried this and it works. You only need to play around with stylesheets.

发布评论

评论列表(0)

  1. 暂无评论