gpt4 book ai didi

javascript - 推特 Bootstrap : Print content of modal window

转载 作者:IT王子 更新时间:2023-10-29 03:03:23 25 4
gpt4 key购买 nike

我正在使用 Bootstrap 开发一个网站,该网站有 28 个模态窗口,其中包含不同产品的信息。我希望能够在打开的模式窗口中打印信息。每个窗口都有一个 id

<!-- firecell panel & radio hub -->
<div class="modal hide fade" id="fcpanelhub">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">X</button>
<h3>5000 Control Panel & Radio Hub</h3>
</div>
<div class="modal-body">
<img src="../site/img/firecell/firecell-panel-info-1.png" alt=""/><hr/>
<img src="../site/img/firecell/firecell-panel-info-2.png" alt=""/><hr/>
<img src="../site/img/firecell/firecell-radio-hub-info-1.png" alt=""/><hr/>
<img src="../site/img/firecell/firecell-radio-hub-info-2.png" alt=""/>
</div>
<div class="modal-footer">
<a href="#" class="btn" data-dismiss="modal">Close</a>
</div>
</div>

因此,如果我在 modal-footer 中添加一个新按钮 - “打印”,并且单击它我希望打印该模态。我说将使用 javascript 是否正确?如果是这样,我如何告诉 javascript 只打印打开模式,而不打印其他模式?

感谢所有帮助。

最佳答案

另一种解决方案

这是一个基于 Bennett McElwee answer 的新解决方案在same question如下所述。

已使用 IE 9 和 10、Opera 12.01、Google Chrome 22 和 Firefox 15.0 进行测试。
jsFiddle example

1.) 将此 CSS 添加到您的站点:

@media screen {
#printSection {
display: none;
}
}

@media print {
body * {
visibility:hidden;
}
#printSection, #printSection * {
visibility:visible;
}
#printSection {
position:absolute;
left:0;
top:0;
}
}

2.) 添加我的 JavaScript 函数

function printElement(elem, append, delimiter) {
var domClone = elem.cloneNode(true);

var $printSection = document.getElementById("printSection");

if (!$printSection) {
$printSection = document.createElement("div");
$printSection.id = "printSection";
document.body.appendChild($printSection);
}

if (append !== true) {
$printSection.innerHTML = "";
}

else if (append === true) {
if (typeof (delimiter) === "string") {
$printSection.innerHTML += delimiter;
}
else if (typeof (delimiter) === "object") {
$printSection.appendChild(delimiter);
}
}

$printSection.appendChild(domClone);
}​

您已准备好打印网站上的任何元素!
只需使用您的元素调用 printElement() 并在完成后执行 window.print()

注意:如果您想在打印之前修改内容(并且仅在打印版本中),请查看此示例(由 waspina 在评论中提供):http://jsfiddle.net/95ezN/121/

也可以使用 CSS 来在打印版中显示附加内容(并且仅在打印版中)。


原解

我认为,您必须通过 CSS 隐藏网站的所有其他部分。

最好将所有不可打印的内容移动到单独的 DIV 中:

<body>
<div class="non-printable">
<!-- ... -->
</div>

<div class="printable">
<!-- Modal dialog comes here -->
</div>
</body>

然后在你的 CSS 中:

.printable { display: none; }

@media print
{
.non-printable { display: none; }
.printable { display: block; }
}

学分转到Greg谁已经回答了类似的问题:Print <div id="printarea"></div> only?

使用 JavaScript 存在一个问题:用户无法看到预览 - 至少在 Internet Explorer 中是这样!

关于javascript - 推特 Bootstrap : Print content of modal window,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12181760/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com