【问题标题】:LImit Characters in Java Script Print output限制 Javascript 打印输出中的字符
【发布时间】:2015-06-09 13:02:37
【问题描述】:

我正在尝试使用 js 文件限制标签上打印的字符数。我遇到问题的代码部分由按钮调用,并从预览框中打印内容。除了我要打印的标题的长度外,一切正常。每当我添加代码以限制我得到的字符数时: TypeError:title.substring 不是函数 标题 = title.substring(0, 11);任何想法

// To allow for the TEMP SLU's to be printed from the main page in alpha order
$("#barcodesku").live('click', function(){
var title=[];
var sku=[];
var first = "";
var second = "";
var indexGlobal = 0;

    $('#acctRecords tbody tr').each(function()
	{
		sku.push($(this).find('#tableSKU').html());
		title.push ($(this).find('#tableTitle').html());
		
                
        });  //end of acctRecords tbody function
    
    //Print the bar codes
		title = title.substring(0,16);
		var x=0;
		for (x=0;x<sku.length;x++){
			
			
                        first += '$("#'+indexGlobal+'").barcode("'+sku[x]+'", "codabar",{barHeight:40, fontSize:30, output:"bmp"});';
			second += '<div class="wrapper"><img src="../images/temp.jpg" /><div id="'+indexGlobal+
			'"></div><div class="fullSKU">&nbsp &nbsp &nbsp '+sku[x]+'</div><br/><div class="title", {title.substring(0,16)}; >'+title[x]+' </div></div><br/><br/>';
			indexGlobal++;
			
 					
 			
					
			}
    var barcode =  window.open('','BarcodeWindow','width=200');
			var html = '<html><head><title>Barcode</title><style type="text/css">'+
   			'.page-break{display:block; page-break-before:always; }'+
			'body{width: 2in;}'+
			'.wrapper{height: 1in;margin-left:10px;margin-top:5px;margin-right:5px;}'+
			'.fullSKU{float: left;}'+
			'.shortSKU{float: right;font-size:25px;font-weight:bold;}'+
			'.title{float: left;}'+
			'</style><script type="text/javascript"src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.js"></script><script type="text/javascript" src="../barcode/jquery-barcode.js"></script><script>$(document).ready(function() {'+first+'window.print();window.close();});</script></head><body>'+second+'</body></html>';
			barcode.document.open();
			barcode.document.write(html);
			barcode.document.close();
			
}); // end of click function

【问题讨论】:

  • 提示:如何从数组中获取子字符串?

标签: javascript text printing limit


【解决方案1】:

从您的代码中,我看到 title 是一个标题数组。您需要执行以下操作:

for (var i = 0; i < title.length; i++) {
    title[i] = title[i].substring(0, 11); 
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多