<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>DOM</title>
<style type="text/css">
div{width:600px;
height:300px;
background:color:#0F0
}
</style>
<script type="text/javascript">
var i=0;
var flag=true;
function gaibian()
{
var spanNode=document.getElementById("abc");
var spanChildren=spanNode.childNodes;
var textNode=spanChildren[0];
if(i%2==0)
{
textNode.nodeValue="我爱祖国";
}
else if (falg=true)
{
textNode.nodeValue="我爱我家";
}
else
{
textNode.nodeValue="我爱家乡";
}
flag=!flag;
i++;
}
</script>
</head>
<body onload="init()">
<span id="abc">我爱我家</span>
<div>
<span id="spanID">这是javascript</span>
<input id="inID" type="text" value="Html Study" />
</div>
<input type="button" value="修改 " onclick="cv()"/>
<input type="button" value="改变" onclick="gaibian()"/>
</body>
</html>
</head>
<body>
</body>
</html>
问题:在网页上能够将我爱我家我爱祖国我爱家乡循环显示求解
var i=0; function gaibian() { var spanNode=document.getElementById("abc"); var spanChildren=spanNode.childNodes; var textNode=spanChildren[0]; if(i%3==0) { textNode.nodeValue="我爱祖国"; } else if (i%3==1) { textNode.nodeValue="我爱家乡"; } else { textNode.nodeValue="我爱我家"; } i=(i+1)%3; }
谢谢
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>DOM</title> <style type="text/css"> div{width:600px; height:300px; background:color:#0F0 } </style> </head> <body> <span id="abc">我爱我家</span> <div> <span id="spanID">这是javascript</span> <input id="inID" type="text" value="Html Study" /> </div> <input type="button" value="修改 " onclick="cv()"/> <input type="button" value="改变" onclick="gaibian()"/> </body> <script type="text/javascript"> var i=0; var flag=true; var replaceValueFun = replaceValue();//初始化 function gaibian(){ var spanNode=document.getElementById("abc"); var spanChildren=spanNode.childNodes; var textNode=spanChildren[0]; //赋值 spanNode.innerHTML = replaceValueFun(); } function replaceValue() { var valueArr = ['我爱祖国', '我爱我家', '我爱家乡'], currentIndex = 0, valueArrLen = valueArr.length; return function(index) { var returnValue = ''; currentIndex = index || currentIndex; //设置index获取对应数组中的值,并且设置此值为初始值。默认初始值为0 currentIndex = currentIndex + 1 > valueArrLen ? 0 : currentIndex; returnValue = valueArr[currentIndex]; currentIndex++; return returnValue; } } </script> </html>
var i=0; function gaibian() { var spanNode=document.getElementById("abc"); var spanChildren=spanNode.childNodes; var textNode=spanChildren[0]; if(i%3==0) { textNode.nodeValue="我爱祖国"; } else if (i%3==1) { textNode.nodeValue="我爱家乡"; } else { textNode.nodeValue="我爱我家"; } i++; }