dom 学习的开始~简单留言1

复制代码
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<script type="text/javascript">

window.onload = function ()
{
    var otext = document.getElementsByTagName('input')[0];
    var obtn = document.getElementsByTagName('input')[1];
    var oul = document.getElementById('ul1');
    var n = 5;
    
    obtn.onclick = function ()
    {
        if(!otext.value)
        {
            return;
        }
        
        var oli = document.createElement('li');
        oli.innerHTML = otext.value;
        
        if(oul.children[0])
        {
            oul.insertBefore(oli,oul.children[0]);
        }
        else
        {
            oul.appendChild(oli);
        }
        
        if(oul.children[n])
        {
            oul.removeChild(oul.children[n]);
        }
        
        
        
        otext.value = '';
    }
};
</script>
</head>

<body>
<input type="text">
<input type="button" value="留言">
<ul id="ul1">
</ul>
</body>
</html>
复制代码

 

posted @   mayufo  阅读(119)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示