我正在尝试将jQuery Print Preview Plugin添加到我的网站中,但我使用AddThis'打印选项。我不确定是否有办法使用AddThis添加打印预览插件。有办法吗?
这是我的代码:
JS:
var $j = jQuery.noConflict();
$j(document).ready(function() {
$j('a.print-preview').printPreview();
});
添加此代码:
<!-- AddThis Peekaboo Toolbox : BEGIN -->
<div class="addthis_toolbox addthis_peekaboo_style
addthis_default_style addthis_label_style addthis_32x32_style">
<a class="addthis_button_more">Share</a>
<ul>
<li><a class="addthis_button_facebook"></a></li>
<li><a class="addthis_button_twitter"></a></li>
<li><a class="addthis_button_google_plusone_share"></a></li>
<li><a class="addthis_button_email"></a></li>
<li><a class="addthis_button_print print-preview"></a></li>
</ul>
</div>
<!-- AddThis Peekaboo Toolbox : END -->
<script type="text/javascript" src="https://s7.addthis.com/js/
300/addthis_widget.js"></script>
当我点击打印时,它会弹出Firefox中的标准打印对话框(或我正在使用的任何浏览器。)
我感觉AddThis JS正在使用类似JavaScript:window.print();
的东西来触发打印对话框,从而绕过了jQuery Print Preview插件。有没有简单的方法来更改AddThis行为以触发打印预览?
答案 0 :(得分:2)
我可以通过删除类并添加自己的类来更改AddThis打印按钮的行为。这是代码:
原始添加此代码:
<!-- AddThis Peekaboo Toolbox : BEGIN -->
<div class="addthis_toolbox addthis_peekaboo_style
addthis_default_style addthis_label_style addthis_32x32_style">
<a class="addthis_button_more">Share</a>
<ul>
<li><a class="addthis_button_facebook"></a></li>
<li><a class="addthis_button_twitter"></a></li>
<li><a class="addthis_button_google_plusone_share"></a></li>
<li><a class="addthis_button_email"></a></li>
<li><a class="addthis_button_print"></a></li>
</ul>
</div>
<!-- AddThis Peekaboo Toolbox : END -->
<script type="text/javascript" src="https://s7.addthis.com/js/300/
addthis_widget.js"></script>
在页面上显示的AddThis代码:
<div class="addthis_toolbox addthis_peekaboo_style
addthis_default_style addthis_label_style addthis_32x32_style">
<!-- Omited code not relevant to print function. -->
<ul style="display: none;"> <!-- Changes to Display Block on Hover -->
<!-- Omited code not relevant to print function. -->
<li>
<a class="addthis_button_print at300b" title="Print" href="#">
<span class=" at300bs at15nc at15t_print"></span>
Print
</a>
</li>
</ul>
编辑代码以更改打印功能并调用打印预览插件:
<div class="addthis_toolbox addthis_peekaboo_style
addthis_default_style addthis_label_style addthis_32x32_style">
<!-- Omited code not relevant to print function. -->
<ul style="display: none;"> <!-- Changes to Display Block on Hover -->
<!-- Omited code not relevant to print function. -->
<li>
<a class="print-preview at-print">
<span class="at-print-span"></span>
Print
</a>
</li>
</ul>
CSS正确显示打印:
.at-print {
padding: 0 2px;
float: left;
padding: 10px 20px !important;
text-decoration: none;
text-overflow: ellipsis;
white-space: nowrap;
}
.at-print-span {
background-position: 0 -576px !important;
background: url("//s7.addthis.com/static/r07/widget006_32x32_top.png")
no-repeat scroll left center transparent;
display: block;
height: 32px !important;
line-height: 32px !important;
overflow: hidden;
width: 32px !important;
float: left;
}
我不确定是否需要所有CSS但它确实有效。
重要提示:
这是您将如何更改打印按钮功能,以便我可以调用jQuery打印预览功能。但是,在将AddThis脚本和“打印预览插件”一起使用时,将抛出错误。必须修改打印预览。
以下是第44行必须完成的操作,您将看到以下代码:
// The frame lives
for (var i=0; i < window.frames.length; i++) {
if (window.frames[i].name == "print-frame") {
var print_frame_ref = window.frames[i].document;
break;
}
}
用以下代码替换上面的代码:
print_frame_ref = print_frame[0].contentWindow.document;
问题解决了。
答案 1 :(得分:0)
有两种选择。一种是添加带有打印图标的自定义服务,只需使用它而不是打印API。
http://support.addthis.com/customer/portal/articles/381245-custom-services
还有一个printfriendly API内置于addthis: