首页 新闻 会员 周边 捐助

javascript

0
悬赏园豆:20 [待解决问题]

<!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>

问题:在网页上能够将我爱我家我爱祖国我爱家乡循环显示求解

生存还是毁灭这是个值得思考的问题的主页 生存还是毁灭这是个值得思考的问题 | 初学一级 | 园豆:86
提问于:2017-10-29 10:08
< >
分享
所有回答(3)
0
  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;
  }
 
hehe_54321 | 园豆:950 (小虾三级) | 2017-10-29 17:57
0
<!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>
momoxsy | 园豆:221 (菜鸟二级) | 2017-10-31 16:56
0
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++;
  }
cxyao | 园豆:202 (菜鸟二级) | 2017-11-03 12:16
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册