js table操作--------table滚动条

简介:
代码:
< html >
< head >
< title > My table </ title >
< style >
.table0 
{
    height
:90%;
}


.table0 caption
{
    width
:100%;
    height
:26px;
    line-height
:26px;
    font-size
:20px;
    font-color
:black;
    font-weight
:900;
    letter-spacing
:5px;
}


.table0 thead td 
{
    text-align
:center;
    vertical-align
:middle;
    height
:20px;
    line-height
:18px;
    font-size
:14px;
    font-color
:black;
    font-weight
:bold;
    padding-top
:2px;
    padding-bottom
:2px;
    border
:#555 1px solid;
    margin
:0px;
}


.table0 tfoot td
{
    text-align
:left;
    vertical-align
:middle;
    height
:20px;
    line-height
:18px;
    font-size
:12px;
    font-color
:black;
    font-weight
:bold;
    padding-top
:2px;
    padding-bottom
:2px;
    padding-left
:12px;
    padding-right
:12px;
    border
:#555 1px solid;
}


.table0 tbody td 
{
    width
:100%;
    height
:auto;
    border
:#555 1px solid;
    padding
:0px;
    margin
:0px;
}


.table1 tbody td 
{
    text-align
:left;
    vertical-align
:middle;
    height
:20px;
    line-height
:18px;
    font-size
:14px;
    font-color
:#444;
    font-weight
:normal;
    padding-top
:2px;
    padding-bottom
:2px;
    padding-left
:12px;
    padding-right
:12px;
    border-right
:#555 1px solid;
    border-bottom
:#555 1px solid;
    overflow
:hidden;
    text-overflow
:ellipsis;
    word-break
:keep-all;
    word-wrap
:normal;
}


</ style >
< script >

function init(){
    theT
=createTable("我的收藏夹",["Group:150","Name:300","URL:200","Visited:100","Modify:100","Delete:100"]);
    
//参数说明:createTable(strCaption,colHeads)
    //strCaption 标题
    //colHeads 是一个array,每个item的格式是 名称:宽度 的字符串
    for(var i=0;i<40;i++){
        theR
=theT.insertRow();
        
for(var j=0;j<7;j++){
            theC
=theR.insertCell();
            theC.innerText
=j;
        }

    }

}



function createTable(strCaption,colHeads){
    
//参数说明:colHeads 是一个array,每个item的格式是 名称:宽度 的字符串
    //生成表格
    oTable=document.createElement("table");
    document.body.insertBefore(oTable);
    
//设置class
    oTable.className="table0";
    
    
with(oTable.style){
        tableLayout
="fixed";
        borderCollapse
="collapse"
        borderSpacing
="0px";
    }

    
//设置变量
    oTCaption=oTable.createCaption();
    oTHead
=oTable.createTHead();
    oTFoot
=oTable.createTFoot();
    
    
//生成标题
    oTCaption.innerText=strCaption;
    
    
//设置列宽
    oTHead.insertRow();
    
for(var i=0;i<colHeads.length;i++){
        tHeadName
=colHeads[i].split(":")[0];
        tHeadWidth
=isNaN(parseInt(colHeads[i].split(":")[1]))?"auto":parseInt(colHeads[i].split(":")[1]);
        theCell
=oTHead.rows[0].insertCell();
        theCell.style.width
=tHeadWidth;
    }

    theCell
=oTHead.rows[0].insertCell();
    theCell.width
=20;
    oTHead.rows[
0].style.display="none";
    
    
//生成表头
    oTHead.insertRow();
    
for(var i=0;i<colHeads.length;i++){
        tHeadName
=colHeads[i].split(":")[0];
        tHeadWidth
=isNaN(parseInt(colHeads[i].split(":")[1]))?"auto":parseInt(colHeads[i].split(":")[1]);
        theCell
=oTHead.rows[1].insertCell();
        theCell.innerText
=tHeadName;
        theCell.style.width
=tHeadWidth;
    }

    theCell
=oTHead.rows[1].insertCell();
    theCell.width
=24;
    
    
//生成表脚
    oTFoot.insertRow();
    theCell
=oTFoot.rows[0].insertCell();
    theCell.innerHTML
="<marquee scrollDelay=50 scrollAmount=2>Copy rights 2005. Hutia presents.</marquee>";
    theCell
=oTFoot.rows[0].insertCell();
    theCell.colSpan
=colHeads.length-1;
    theCell
=oTFoot.rows[0].insertCell();
    theCell.width
=20;
    
    
//生成主体
    oTable.all.tags("Tbody")[0].insertRow();
    theCell
=oTable.all.tags("Tbody")[0].rows[0].insertCell();
    theCell.colSpan
=colHeads.length+1;
    oMain
=document.createElement("DIV");
    theCell.insertBefore(oMain);
    
with(oMain.style){
        width
="100%";
        height
="100%";
        overflowY
="auto";
        overflowX
="hidden";
        margin
="0px";
        marginLeft
="-1px";
    }

    
    oTBody
=document.createElement("table");
    oMain.insertBefore(oTBody);
    oTable.oTBody
=oTBody;
    
//禁止选择
    oTCaption.onselectstart=oTHead.onselectstart=oTFoot.onselectstart=function(){return(false);}
    
    
//设置class
    oTBody.className="table1";
    
with(oTBody.style){
        width
=oTable.offsetWidth-15;
        tableLayout
="fixed";
        borderCollapse
="collapse"
        borderSpacing
="0px";
        padding
="0px";
        margin
="0px";
    }

    
    
//初始化列宽
    oTBody.insertRow();
    
for(var i=0;i<colHeads.length;i++){
        tHeadWidth
=isNaN(parseInt(colHeads[i].split(":")[1]))?"auto":parseInt(colHeads[i].split(":")[1]);
        theCell
=oTBody.rows[0].insertCell();
        theCell.style.width
=tHeadWidth;
    }

    oTBody.rows[
0].style.display="none";
    
    
return(oTBody);
}


function insertRow(){
    
var intL=oTBody.rows.length;
    theRow
=oTBody.insertRow();
    theRow.index
=intL;
    theRow.onmouseover
=rowOnMouseOver;
    theRow.onmouseout
=rowOnMouseOut;
    theRow.onclick
=rowOnClick;
    
for(var i=0;i<colHeads.length;i++){
        theCell
=theRow.insertCell();
        theCell.tabIndex
=0;
        theCell.hideFocus
=true;
        theCell.colIndex
=i;
        theCell.onmouseover
=function(){this.focus();};
        theCell.onmouseout
=cellOnBlur;
        theCell.onfocus
=cellOnFocus;
        theCell.onblur
=cellOnBlur;
    }

    theRow.cells[
0].innerText=strGroup?strGroup:"ROOT";
    theRow.cells[
1].innerText=strName?strName:"Untitled Document.";
    theRow.cells[
2].innerText=strURL?strURL:"Unspecified URL";
    theRow.cells[
3].innerHTML=strVisited?"Yes".fontcolor("red"):"Unknow";
    theRow.cells[
4].innerHTML=strModify?"Yes".fontcolor("red"):"No".fontcolor("Green");
    theRow.cells[
5].innerHTML="Delete".fontcolor("red");
}


</ script >
</ head >
< body  onload ="init();" >

</ body >
</ html >
本文转自博客园执着的笨蛋的博客,原文链接:js table操作--------table滚动条 ,如需转载请自行联系原博主。
目录
相关文章
|
1月前
|
JavaScript 前端开发
JavaScript操作DOM元素
JavaScript操作DOM元素
12 1
|
1月前
|
JavaScript 前端开发
如何使用 JavaScript 操作 DOM?
如何使用 JavaScript 操作 DOM?
13 0
|
1月前
|
移动开发 前端开发 JavaScript
编程笔记 html5&css&js 005 网页上都有哪内容、形式和操作
编程笔记 html5&css&js 005 网页上都有哪内容、形式和操作
|
4天前
|
JavaScript 前端开发 UED
深入解析JavaScript原生操作DOM技术
【4月更文挑战第22天】本文深入探讨JavaScript原生DOM操作技术,包括使用`getElement*`方法和CSS选择器获取元素,借助`createElement`与`appendChild`动态创建及插入元素,修改元素内容、属性和样式,以及删除元素。通过掌握这些技术,开发者能实现页面动态交互,但应注意避免过度操作DOM以优化性能和用户体验。
|
11天前
|
存储 JavaScript 前端开发
JavaScript DOM 操作:解释一下 cookie、sessionStorage 和 localStorage 的区别。
Cookie是服务器发送至客户端的文本信息,会随每个请求发送回服务器,适合控制会话状态但可能暴露隐私。SessionStorage仅在当前会话中存储数据,关闭浏览器后清除,适合临时存储如登录状态。LocalStorage则持久保存数据,即使关闭浏览器也不会清除,适用于存储长期设置。三种方式各有侧重,应按需求选择。
15 0
|
11天前
|
JavaScript 前端开发 安全
JavaScript DOM 操作:解释一下浏览器的同源策略。
**同源策略**是浏览器安全基石,它阻止脚本跨不同协议、域名或端口访问资源,防止恶意行为。例如,HTTP页面无法直接用JS获取HTTPS页面内容。**CORS**允许跨域请求,但需服务器配合设置,通过`document.domain`属性可配置,但仍受限于服务器配置。
14 4
|
18天前
|
JavaScript
【归总】原生js操作浏览器hash、url参数参数获取/修改方法合集
【归总】原生js操作浏览器hash、url参数参数获取/修改方法合集
|
1月前
|
JavaScript 前端开发
JavaScript操作BOM简单案例
JavaScript操作BOM简单案例
9 1
|
1月前
|
JavaScript 前端开发
JavaScript操作BOM对象
JavaScript操作BOM对象
9 1
|
1月前
|
JSON JavaScript 前端开发
盘点3个可以操作JavaScript的Python库
盘点3个可以操作JavaScript的Python库
31 0