js笔记

一、js整数转换为小数 

var a = 1;

alert(a.toFixed(1))  显示 a=1.0;

 

二、取同名的id

 var serviceAppends = document.getElementsByName("serviceAppend");
 for(i=0; i < serviceAppends.length; i++)
 {
      alert(serviceAppends[i].id);

      alert(serviceAppends[i].value));
 }

 

三、js控制表格的合并与拆分,下面是针对第一列进行合并,如果对多列合并,修改方法。

function _combincells() { 
	var rows = document.getElementsByTagName('tr');
	
	//开始扫描表格
	var ar_startRI = new Array();
	var ar_count = new Array();
	var CID = '';
	for(var i = 1; i < rows.length; i ++) {
		var tds = rows[i].getElementsByTagName('td');
		if(CID == '') {
			CID = tds[0].innerHTML;
			var count = 1;
			var startRI = 1;
			continue;
		}else {
			if(tds[0].innerHTML == CID) {
				count ++;
			}else {
				if (count >= 2) {
					ar_startRI.push(startRI);
					ar_count.push(count);
				}
				CID = tds[0].innerHTML;
				count = 1;
				startRI = i;
			}
		}
	}
	if (count >= 2) {
		ar_startRI.push(startRI);
		ar_count.push(count);
	}
	//开始合并
	for(i = 0; i < ar_startRI.length; i ++) {
		var rowIdx = ar_startRI[i];
		var tds = rows[rowIdx].getElementsByTagName('td');
		tds[0].rowSpan = ar_count[i];
		for(var j = rowIdx + 1; j < rowIdx + ar_count[i]; j ++) {
			rows[j].deleteCell(0);
		}
	}  	
}

 

四、字符串替换

serviceAppends[i].id.replace(new RegExp("\\.", "gm"), "_");

你可能感兴趣的:(js)