Javascript 如何使用jquery在弹出窗口中打开pdf文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34196293/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to open a pdf file in a popup window with jquery
提问by Harish
My code throws a JS error, offsetParent is not set -- cannot scroll. I tried position: relative;
but it still shows the same error.
我的代码抛出一个 JS 错误,未设置 offsetParent - 无法滚动。我试过了,position: relative;
但它仍然显示相同的错误。
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.9/jquery-ui.js" type="text/javascript"></script>
<link href="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.9/themes/blitzer/jquery-ui.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
$(document).ready(function() {
$('#btnShow').click(function(){
$("#dialog").dialog();
});
});
</script>
<a href="#" id="trigger">this link</a>
<div id="dialog" style="display: none; position: relative; height: 4em; overflow: scroll;">
<div>
<iframe src="reports/my_pdf.pdf"></iframe>
</div>
</div>
回答by Michal C
Try to use "lazy loading":
尝试使用“延迟加载”:
<script type="text/javascript">
$(document).ready(function() {
$('#btnShow').click(function(){
$("#dialog").dialog();
$("#frame").attr("src", "reports/my_pdf.pdf");
});
});
</script>
<a href="#" id="btnShow">this link</a>
<div id="dialog" style="display: none;">
<div>
<iframe id="frame"></iframe>
</div>
</div>
回答by Harish
Rather than jquery you can simply do this by using Javascript
而不是 jquery,你可以简单地使用 Javascript 来做到这一点
You can use window.open()
method and insert the pdf file in it window.open("path/for/pdffile")
and give width, height and its position
您可以使用window.open()
方法并在其中插入pdf文件window.open("path/for/pdffile")
并给出宽度,高度及其位置
window.open("path/for/pdffile", "width=500,height=500,top=100,left=500")
You can insert the above code in a function and call it when a html element is clicked .
您可以在函数中插入上述代码,并在单击 html 元素时调用它。
That's it your pdf file will be opened in a pop up window
就是这样,您的 pdf 文件将在弹出窗口中打开