简体   繁体   English

我要打印页面时javascript中断

[英]javascript breaks when I want to print a page

So I created this webpage using JQuery. 因此,我使用JQuery创建了此网页。 I use this code to resize the images on my page so that they fit in squares. 我使用此代码来调整页面上图像的大小,以使其适合正方形。

<script type="text/javascript">
  $(document).ready(function(){
    var height = $('.square').width();
    $('.square').css('height', height); 
    window.onresize = function(event) {
      var height = $('.square').width();
      $('.square').css('height', height);
   }
 });
</script>

This works perfectly. 这很完美。 The images resize to squares when I load the page or when i resize my browser window. 当我加载页面或调整浏览器窗口大小时,图像会调整为正方形。 The problem is when I go and press print page (I'm using google Chrome). 问题是当我去按打印页面时(我使用的是谷歌浏览器)。 The printer preview loads the page as if these two scripts don't exist. 打印机预览将加载页面,就好像这两个脚本不存在一样。 So the images are their original sizes. 因此,图像是其原始大小。

Any Ideas ? 有任何想法吗 ?

If you just want to keep images square, you might not even need javascript, the below gives you an example, the bonus it's responsive.. 如果您只是想使图像保持方形,则可能甚至不需要JavaScript,下面提供了一个示例,它具有响应能力。

 .pic-wrapper { width: 40%; float: left; margin-right: 20px; } .pic { width: 100%; padding-bottom: 100%; background-image: url('http://i.imgur.com/uTDpE6J.jpg') } 
 <div class="pic-wrapper"> <div class="pic"> </div> </div> <div class="pic-wrapper"> <div class="pic"> </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用javascript打印时,如果页面中断时如何添加标题? - how to add header when page breaks when print using javascript? 想要使用JavaScript打印页面的特定部分 - Want to print a particular section of a page using JavaScript 有什么办法用javascript(印刷媒体)进行分页符吗? - Any way to do page breaks with javascript (print media)? 我想将所有 api 结果打印到页面 - I Want To Print all the api result to page 使用Ajax更新html时Javascript中断 - Javascript breaks when I update html with Ajax JavaScript Window打印-我希望它打印特定的div - JavaScript WIndow Print - I want it to print a specific div 当我想打印数组中的所有项目时,javascript getItem 打印最后一个数组 - javascript getItem prints last array when i want to print all items in array 我想在按下打印按钮时得到打印对话框吗? - I want to get print dialog box when press print button? 我的页面生成多个具有相同 ID 和相同 class 的 div。 我想用相同的 javascript 按钮打印它们 - My page generates multiple divs with same ID and same class. I want to print them all with the same javascript button 我想通过 JavaScript 通过用户输入在我的 DOM 中打印一个 Rectangle,但是当我运行此代码时,我得到 'str' is not defined - I want to print a Rectangle in my DOM by JavaScript by taking a user input, but when I run this code I got 'str' is not defined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM