Dom高级二

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

DOM高级(二)

  1. 实现多关键词搜索
  2. 表格排序实例
  3. appendChild 的新用法
  4. 用sort()对数组进行排序
<style>
#ul1 {background:red;}
#ul2 {background:yellow;}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script>
window.onload=function ()
{
    var oBtn=document.getElementById('btn1');
    var oUl=document.getElementById('ul1');
    
    oBtn.onclick=function ()
    {
        var aLi=oUl.getElementsByTagName('li');
        var arr=[];
        var i=0;
        
        //1.转成数组
        for(i=0;i<aLi.length;i++)
        {
            arr[i]=aLi[i];
        }
        
        //2.数组排序
        arr.sort(function (li1, li2){
            return parseInt(li1.innerHTML)-parseInt(li2.innerHTML);
        });
        
        //3.重新插入
        for(i=0;i<arr.length;i++)
        {
            oUl.appendChild(arr[i]);
        }
    };
};
</script>
</head>

<body>
<input id="btn1" type="button" value="排序Li" />
<ul id="ul1">
    <li>32</li>
    <li>12</li>
    <li>87</li>
    <li>9</li>
    <li>18</li>
</ul>
</body>
</html>
  1. 用sort()方法对表格进行排序实例
  2. 用!进行简易写法做判断
  3. 表单应用:action、method、name、onsubmit、getElementsByName、onreset
<script>
window.onload=function ()
{
    var oForm=document.getElementById('form1');
    var oUser=document.getElementsByName('user')[0];
    var oPass=document.getElementsByName('pass')[0];
    
    oForm.onsubmit=function ()
    {
        if(oUser.value=='' || oPass.value=='')
        {
            alert('您填错了');
            return false;
        }
    };
    
    oForm.onreset=function ()
    {
        /*if(confirm('是否要清空?'))
        {
            return true;
        }
        else
        {
            return false;
        }*/
        
        return confirm('是否要清空?');
    };
};
</script>
</head>

<body>
<form id="form1" action="http://www.miaov.com/" method="get">
    用户名:<input type="text" name="user" />
    密码:<input type="password" name="pass" />
    <input type="submit" value="提交" />
    <input type="reset" value="重置" />
</form>
</body>
  1. return confirm 的简化版
  2. 表单内容验证相关知识


    image.png
上一篇 下一篇

猜你喜欢

热点阅读