如何使用javascript for循环更改文本颜色?

时间:2013-02-14 03:55:46

标签: javascript

当有人点击文字时,它会变为红色,然后点击其他文字会返回黑色。 我在下面做了一个例子,但是如何使用for循环缩短它?

    <html>
    <head>
    <title>My little test page</title>
    </head>
    <body id="body">
        <div id="myid">Hello Here !!</div><br>
        <div id="myid2">Hello There !!</div><br>
        <div id="myid3">Hello !!</div><br>
        ......many div......
    </body>
    </html>

    <script language="javascript">
    function changeColor1() {
    document.getElementById("myid").className = "red";
    document.getElementById("myid2").className = "";
    document.getElementById("myid3").className = "";
    }

    function changeColor2() {
    document.getElementById("myid").className = "";
    document.getElementById("myid2").className = "red";
    document.getElementById("myid3").className = "";
    }

    function changeColor3() {
    document.getElementById("myid").className = "";
    document.getElementById("myid2").className = "";
    document.getElementById("myid3").className = "red";
    }

    function init() {
    document.getElementById("myid").onclick = changeColor1;
    document.getElementById("myid2").onclick = changeColor2;
    document.getElementById("myid3").onclick = changeColor3;
    }

    window.onload = init();
    </script>

4 个答案:

答案 0 :(得分:1)

试试这个。

    <html>
        <head>
        <title>My little test page</title>
        </head>

 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
      <script language="javascript">
     $(".divid").click(function(e){
         $(".divid").css('color', '');
         $(this).css('color', 'red');

       });
       </script>
 <body id="body">
 <div id="myid" class="divid">Hello Here !!</div><br>
 <div id="myid2" class="divid">Hello There !!</div><br>
 <div id="myid3" class="divid">Hello !!</div><br>
 ......many div......
 </body>
 </html>

示例jsfiddle

答案 1 :(得分:1)

为所有div添加属性名称,例如

<div id='myid' name='colorchangingdiv[]'>Hello</div>
<div id='myid2' name='colorchangingdiv[]'>Hello</div>
....

现在在你的init函数中

function init() {
var allDivs = document.getElementsByName('colorchangingdivs[]');

    for( var i =0; i < allDivs.length; ++i )
    {
       allDivs[i].onClick = changeColor(this);
    }
}

在你的变色功能

function changeColor( obj )
{
    var allDivs = document.getElementsByName('colorchangingdivs[]');

    for( var i =0; i < allDivs.length; ++i )
    {
       allDivs[i].style.color = '';
    }

    // Now set the color to the obj passed
    obj.style.color = 'red';
 }

希望有所帮助。

答案 2 :(得分:0)

<html>
<head>
<style>
  .red{
  color:red;
 }
</style>
<title>My little test page</title>
<script type="text/javascript" src="jquery.js">
<script language="javascript">
  //use jquery for change your color
  $(document).ready(function(){
   $(".myclass").click(function(){
    $(".myclass").removeClass("red");
    $(this).addClass("red");
   });
  });
</script>
</head>
<body id="body">
    <div class="myclass">Hello Here !!</div><br>
    <div class="myclass">Hello There !!</div><br>
    <div class="myclass">Hello !!</div><br>
    ......many div......
</body>
</html>

答案 3 :(得分:0)

为方便起见,请使用类代替上述div标签的id属性,或者除了以上div标签的id属性之外:

<!DOCTYPE html>
<html>
   <head>
      <title>My little test page</title>
   </head>
   <body id="body">
      <div class="mytext">Hello Here !!</div><br>
      <div class="mytext">Hello There !!</div><br>
      <div class="mytext">Hello !!</div><br>

      <script>
        var myselector = 'div.mytext';
        function changeColor( e ){
          var x=document.querySelectorAll(myselector);
          for(i=0;i<x.length;i++){
            x[i].style.color = '';
          }
          e.target.style.color = 'red';
        }      
        function init(){
          var x=document.querySelectorAll(myselector);
          for(i=0;i<x.length;i++){
            x[i].addEventListener("click", function(e){
              changeColor(e);
            });
          }
        }
        window.onload = init();
      </script>
   </body>
</html>