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

时间:2023-02-11 13:40:13
<!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>