Dom高级一

2018-04-19  本文已影响0人  Monee121

DOM高级(一)

  1. 获取表格元素:tBodies、rows、cells
<script>
window.onload=function ()
{
    var oTab=document.getElementById('tab1');
    
    //alert(oTab.getElementsByTagName('tbody')[0].getElementsByTagName('tr')[2].getElementsByTagName('td')[1].innerHTML);
    alert(oTab.tBodies[0].rows[2].cells[1].innerHTML);
};
</script>
</head>

<body>
<table id="tab1" border="1" width="400">
    <thead>
        <td>ID</td>
        <td>姓名</td>
        <td>操作</td>
    </thead>
    <tbody>
        <tr>
            <td>1</td>
            <td>Blue</td>
            <td></td>
        </tr>
        <tr>
            <td>2</td>
            <td>Leo</td>
            <td></td>
        </tr>
    </tbody>
</table>
</body>
  1. 表格中 tBodies 的意义
  2. 表格隔行换色实例
<script>
window.onload=function ()
{
    var oTab=document.getElementById('tab1');
    var i=0;
    
    for(i=0;i<oTab.tBodies[0].rows.length;i++)
    {
        oTab.tBodies[0].rows[i].style.background=i%2?'#CCC':'';
    }
};
</script>
</head>
<body>
  1. 表格添加、删除实例
<script>
window.onload=function ()
{
    var oTab=document.getElementById('tab1');
    var oBtn=document.getElementById('btn1');
    var oTxt=document.getElementById('txt1');
    
    oBtn.onclick=function ()
    {
        var oTr=document.createElement('tr');
        var oTd=null;
        
        oTd=document.createElement('td');
        oTd.innerHTML=oTab.tBodies[0].rows.length+1;
        oTr.appendChild(oTd);
        
        oTd=document.createElement('td');
        oTd.innerHTML=oTxt.value;
        oTr.appendChild(oTd);
        
        oTd=document.createElement('td');
        oTd.innerHTML='&nbsp;';
        oTr.appendChild(oTd);
        
        oTab.tBodies[0].appendChild(oTr);
    };
};
</script>
</head>

<body>
<input id="txt1" type="text" />
<input id="btn1" type="button" value="添加" />
<table id="tab1" border="1" width="400">
    <thead>
        <td>ID</td>
        <td>姓名</td>
        <td>操作</td>
    </thead>
    <tbody>
        <tr>
            <td>1</td>
            <td>Blue</td>
            <td></td>
        </tr>
....

删除
<script>
window.onload=function ()
{
    var oTab=document.getElementById('tab1');
    var oBtn=document.getElementById('btn1');
    var oTxt=document.getElementById('txt1');
    var iNowId=oTab.tBodies[0].rows.length+1;
    
    oBtn.onclick=function ()
    {
        var oTr=document.createElement('tr');
        var oTd=null;
        
        oTd=document.createElement('td');
        oTd.innerHTML=iNowId++;
        oTr.appendChild(oTd);
        
        oTd=document.createElement('td');
        oTd.innerHTML=oTxt.value;
        oTr.appendChild(oTd);
        
        oTd=document.createElement('td');
        oTd.innerHTML='<a href="javascript:;">删除</a>';
        oTr.appendChild(oTd);
        
        oTd.getElementsByTagName('a')[0].onclick=function ()
        {
            oTab.tBodies[0].removeChild(this.parentNode.parentNode);
        };
        
        oTab.tBodies[0].appendChild(oTr);
    };
};
</script>
</head>

<body>
<input id="txt1" type="text" />
<input id="btn1" type="button" value="添加" />
<table id="tab1" border="1" width="400">
    <thead>
        <td>ID</td>
        <td>姓名</td>
        <td>操作</td>
    </thead>
    <tbody>
        <tr>
            <td>1</td>
            <td>Blue</td>
            <td></td>
        </tr>
  1. 修复删除的问题
  2. 表格搜索实例
<script>
window.onload=function ()
{
    var oBtn=document.getElementById('btn1');
    var oTab=document.getElementById('tab1');
    var oTxt=document.getElementById('txt1');
    
    oBtn.onclick=function ()
    {
        var i=0;
        
        for(i=0;i<oTab.tBodies[0].rows.length;i++)
        {
            var sValueInTab=oTab.tBodies[0].rows[i].cells[1].innerHTML.toLowerCase();
            var sValueInTxt=oTxt.value.toLowerCase();
            if(sValueInTab==sValueInTxt)
            {
                oTab.tBodies[0].rows[i].style.background='yellow';
            }
            else
            {
                oTab.tBodies[0].rows[i].style.background='';
            }
        }
    };
};
</script>
  1. 消除大小写字母的问题
<script>
var a='aaa';
var b='AaA';

alert(a.toLowerCase()==b.toLowerCase());
</script>
  1. 解决多个关键字搜索的问题
<script>
window.onload=function ()
{
    var oBtn=document.getElementById('btn1');
    var oTab=document.getElementById('tab1');
    var oTxt=document.getElementById('txt1');
    
    oBtn.onclick=function ()
    {
        var i=0;
        
        for(i=0;i<oTab.tBodies[0].rows.length;i++)
        {
            var sValueInTab=oTab.tBodies[0].rows[i].cells[1].innerHTML.toLowerCase();
            var sValueInTxt=oTxt.value.toLowerCase();
            var arr=sValueInTxt.split(' ');
            
            var bFound=false;
            
            for(var j=0;j<arr.length;j++)
            {
                if(sValueInTab.search(arr[j])!=-1)
                {
                    bFound=true;
                    break;
                }
            }
            
            if(bFound)
            {
                oTab.tBodies[0].rows[i].style.background='yellow';
            }
            else
            {
                oTab.tBodies[0].rows[i].style.background='';
            }
        }
    };
};
</script>
</head>

<body>
<input id="txt1" type="text" />
<input id="btn1" type="button" value="搜索" />
<table id="tab1" border="1" width="400">
    <thead>
        <td>ID</td>
        <td>姓名</td>
        <td>操作</td>
    </thead>
    <tbody>
        <tr>
            <td>1</td>
            <td>Blue</td>
            <td></td>
        </tr>
        <tr>
            <td>2</td>
            <td>Leo</td>
            <td></td>
        </tr>
<title>高亮显示</title>
<script>
window.onload=function ()
{
    var oTab=document.getElementById('tab1');
    var oldBgColor='';
    var i=0;
    
    for(i=0;i<oTab.tBodies[0].rows.length;i++)
    {
        oTab.tBodies[0].rows[i].style.background=i%2?'#CCC':'';
        
        oTab.tBodies[0].rows[i].onmouseover=function ()
        {
            oldBgColor=this.style.background;
            this.style.background='yellow';
        };
        
        oTab.tBodies[0].rows[i].onmouseout=function ()
        {
            this.style.background=oldBgColor;
        };
    }
};
</script>
</head>

<body>
<table id="tab1" border="1" width="400">
    <thead>
        <td>ID</td>
        <td>姓名</td>
        <td>操作</td>
    </thead>
    <tbody>
        <tr>
            <td>1</td>
            <td>Blue</td>
            <td></td>
        </tr>
        <tr>
            <td>2</td>
            <td>Leo</td>
            <td></td>
        </tr>
        <tr>
            <td>3</td>
            <td>莫莫</td>
            <td></td>
        </tr>
        <tr>
            <td>4</td>
            <td>aaa</td>
            <td></td>
        </tr>
        <tr>
            <td>5</td>
            <td>bbb</td>
            <td></td>
        </tr>
        <tr>
            <td>6</td>
            <td>ccc</td>
            <td></td>
        </tr>
    </tbody>
</table>
</body>
<title>A在字符串里找到没有</title>
<script>
var str='blue leo';
var arr=str.split(' ');// arr=['blue','leo']

var str2='leo is a person';

var bFound=false;//没有找到

for(i=0;i<arr.length;i++)
{
    if(str2.search(arr[i])!=-1) //(字符串在数组里没有)非,就是有,然后设为true,
    {
        bFound=true;
        break;
    }
}

alert(bFound);
</script>
<script>
var str='abcdef';

alert(str.search('u')); //-1  ,如果有,第一位就返回0
</script>
上一篇 下一篇

猜你喜欢

热点阅读