繁体   English   中英

打印时如何计算位置固定div标签的数量?

[英]How to calculate number of position fixed div tag while printing?

var bottom = 0; /* Position of first page number - 0 for bottom of first page */
var pagNum = 2; /* First sequence - Second number */
$(document).ready(function() {
  /* For each 10 paragraphs, this function: clones the h3 with a new page number */
  $("p:nth-child(10n)").each(function() {
    bottom -= 100;
    botString = bottom.toString();
    var $counter = $('h3.pag1').clone().removeClass('pag1');
    $counter.css("bottom", botString + "vh");
    numString = pagNum.toString();
    $counter.addClass("pag" + numString);
    ($counter).insertBefore('.insert');
    pagNum = parseInt(numString);
    pagNum++; /* Next number */
  });
  var pagTotal = $('.pag').length; /* Gets the total amount of pages by total classes of paragraphs */
  pagTotalString = pagTotal.toString();
  $("h3[class^=pag]").each(function() {
    /* Gets the numbers of each classes and pages */
    var numId = this.className.match(/\d+/)[0];
    document.styleSheets[0].addRule('h3.pag' + numId + '::before', 'content: "Page ' + numId + ' of ' + pagTotalString + '";');
  });
});

$('.print').on('click', function() {
  window.print();
  return false;
});

上面的代码每10个<p>标签将在页码上添加一个。 例如

// if one <p> tag
Page 1 of 1

// if ten <p> tag
Page 1 of 10

我有一个固定的<div>标记,该标记将显示在每个打印页面的顶部。 每当它显示在下一个打印页面上时,我都希望获取固定位置的<div>标签的数量。

反正有这样做吗?

下面的代码将获取所有<div>元素,并过滤掉所有不带position: fixed元素position: fixed

jQuery的.css()的吸气剂是利用JavaScript的的getComputedStyle()因此它适用于内联 CSS-应用的样式。

 var fixedDivCount = $("div").filter(function() { return $(this).css('position') == 'fixed'; }).length; console.log(fixedDivCount); 
 .two { position: fixed; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div style="position: fixed;">1</div> <div class="two">2</div> <div></div> 

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM