我似乎无法通过JavaScript更改类值!

时间:2011-05-27 00:32:13

标签: javascript css ajax class javascript-events

我一直试图这样做无济于事:(从我研究的内容来看,有两种方法可以改变JavaScript中声明的类值:

的document.getElementById(ⅴ).setAttribute(属性,值);

document.getElementById(v).className = value;

但是,我试过了两个,他们正在做深蹲:(我昨天浪费了一整天才弄清楚这一点无济于事:(

这是我的代码:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">
<html>
  <head>
    <title>Checklist</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <link rel="stylesheet" type="text/css" href="/systems_hr/Style%20Library/globalstyles_test.css">
    <style type="text/css">
      #innerframe {
                width: 100%;
                height: 63em;
        }
    </style>
    <script type="text/javascript" src="/systems_hr/Style%20Library/JavaScripts/styles.js"></script>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.13/jquery-ui.min.js"></script>    
    <script type="text/javascript">    
      /* I'll clean this one up. Didn't want to play nice with styles.js
      var x = 0;
      var p = new Array(3) 

      for (x = 1; x < 4; x++)
      {
        p[x] = new Image();
        p[x].src = "/systems_hr/onboarding/Custom%20Pages/Checklist%20EN/images/id1-" + x + ".png";
      } 
      */

      var p0 = new Image();
      p0.src = "images/id1.png";

      var p1 = new Image();
      p1.src = "images/id1-1.png";

      var p2 = new Image();
      p2.src = "images/id1-2.png";

      var p3 = new Image();
      p3.src = "images/id1-3.png";  

      var p4 = new Image();
      p4.src = "images/id2.png";

      var p5 = new Image();
      p5.src = "images/id2-1.png";

      var p6 = new Image();
      p6.src = "images/id2-2.png";

      var p7 = new Image();
      p7.src = "images/id2-3.png";    

      var p8 = new Image();
      p8.src = "images/id3.png";

      var p9 = new Image();
      p9.src = "images/id3-1.png";

      var p10 = new Image();
      p10.src = "images/id3-2.png";

      var p11 = new Image();
      p11.src = "images/id3-3.png";       

      var p12 = new Image();
      p12.src = "images/id4.png";

      var p13 = new Image();
      p13.src = "images/id4-1.png";

      var p14 = new Image();
      p14.src = "images/id4-2.png";

      var p15 = new Image();
      p15.src = "images/id4-3.png"; 

      function changeFrame (frameSRC)
      {
        var myFrame = document.getElementById('fraContent'); 
        myFrame.contentWindow.location =  frameSRC;
      }

      function changeTab(tab)
      { 
        alert(tab);  
        switch(tab)     
        {
          case 0:         
            document.getElementById("week1").className = "active";
            test('week1');
            document.getElementById("month1").className = "active"; 
            test('month1');        
            document.getElementById("day1").className = "current";   
            test('day1');  
          case 1:
            document.getElementById("day1").className = "active";
            document.getElementById("month1").className = "active";         
            document.getElementById("week1").className = "current";
          case 2:
            document.getElementById("week1").className = "active";
            document.getElementById("day1").className = "active";         
            document.getElementById("month1").className = "current";
        }  

      }

      function test(tab)
      {
        alert(document.getElementById.id + " " + document.getElementById(tab).className)
      }
    </script>

  </head>
  <body>
  <table border="0" width="100%">
  <tr>
    <td colspan="2" align="left">
      <div id="navcontainer">
        <ul id="navlist">
          <li><a href="#" class="active" id="day1" onclick="changeTab(0);">FIRST DAY</a></li>
          <li><a href="#" class="active" id="week1" onclick="changeTab(1);test('day1');test('week1');test('month1');">FIRST WEEK</a></li>
          <li><a href="#" class="active" id="month1" onclick="changeTab(2);">FIRST 30-DAYS</a></li>
        </ul>
      </div>
      <div id="page_viewer">
          <table border="0" width="1020px" cellpadding="0" cellspacing="0">
          <tr>
            <td>
              <img src="images/id1.png" style="border: 0px; width: 1000px; height: 72px;" alt="" usemap="#imgTabMap" name="imgTab">
                <map id="imgTabMap" name="imgTabMap">
                  <area shape="rect" alt="" title="" coords="7,11,166,60" href="" name="area1" onclick="changeFrame('D1.html')">
                  <area shape="rect" alt="" title="" coords="206,10,365,59" href="" target="_self" onMouseOver="changeImageSrc('imgTab','p1')" onMouseOut="changeImageSrc('imgTab','p0')" name="area2" onclick="changeFrame('D2.html')">
                  <area shape="rect" alt="" title="" coords="405,11,566,59" href="" target="_self" onMouseOver="changeImageSrc('imgTab','p2')" onMouseOut="changeImageSrc('imgTab','p0')" name="area3" onclick="changeFrame('D3.html')">
                  <area shape="rect" alt="" title="" coords="605,10,763,60" href="" target="_self" onMouseOver="changeImageSrc('imgTab','p3')" onMouseOut="changeImageSrc('imgTab','p0')" name="area4" onclick="changeFrame('D4.html')">
                  <area shape="rect" alt="" title="" coords="805,9,963,60" href="" target="_self" onMouseOver="changeImageSrc('imgTab','p4')" onMouseOut="changeImageSrc('imgTab','p0')" name="area5">
                  <!-- Created by Online Image Map Editor (http://www.maschek.hu/imagemap/index) -->
                </map>

            </td>
          </tr>
          <tr>
            <td width="100%">
              <iframe name="fraContent" id="innerframe" src="D1.html" frameborder="0" scrolling="no" width="1000px"></iframe>
            </td>
          </tr>
          </table>
      </div>
      </td>
    </tr>
  </table>
  </body>
</html>

我知道有很多粗糙的边缘代码,但是我很紧张,而且我还没精通JavaScript语法。

以下是我的代码的具体内容以及我要完成的内容:

我为网站使用全局css代码,因此我的大多数声明都存储在那里。我现在已经在内部制作了javascript代码(可以这么说)。但是,如果这样可行,我计划将其添加到我的站点的全局JavaScript存储库( styles.js 一个)。我添加了jQuery,以便稍后使用它(我还没有,因为我现在只是想学习它)。

好的,现在从你看到的,这段代码只是一个标签界面。这是我的标签的CSS代码:

/* CSS Tabs */
#navlist {
        padding: 3px 0;
        margin-left: 0;
        border-bottom: 1px solid #778;
        font: bold 12px Verdana, sans-serif;
}

#navlist li {
        list-style: none;
        margin: 0;
        display: inline;
}

#navlist li a.active {
        padding: 3px 0.5em;
        margin-left: 3px;
        border: 1px solid #778;
        border-bottom: none;
        background: #DDE;
        text-decoration: none;
}

#navlist li a:link { color: #448; }
#navlist li a:visited { color: #448; }

#navlist li a.active:hover {
        color: #000;
        background: #AAE;
        border-color: #227;
}

#navlist li a.current {
        padding: 3px 0.5em;
        margin-left: 3px;
        border: 1px solid #778;
        border-bottom: 1px solid white;
        text-decoration: none;
        background: white;
        cursor: text;
}

#navlist li a.current:hover {
        background: white;
        border-bottom: 1px solid white;
        cursor: text;
        color: #448;
}

#navlist div #content {
        font-size: 1em;
}

选项卡应该在点击时更改iframe的内容。有问题的JavaScript代码的工作就是简单地改变点击标签的外观。在上面的CSS代码中,这是名为“current”的类,而默认样式是“active”类。

应该处理更改的代码是changeTab(tab)。首先,我将所有选项卡设置为默认类“active”。当我单击选项卡时,changeTab应该启动(也是onLoad,但我没有先添加)。这就是我遇到问题的地方。目标选项卡上的类值没有变化。

首先,我添加了一些catch代码,以查看onClick事件是否正常运行(我在changeTab函数中添加警报代码的原因)。然后,我添加了一个代码,让我可以看到类属性的最终值( test(tab)函数)。它应该显示标签的Id和它的类属性的值。

当我试用它时,我确实在changeTab中获取了传递的参数,它告诉我onClick正在运行。但是测试告诉我标签是未定义的。因此,无论我点击哪个标签,“当前”类都将应用于最后一个标签(“month1”)。

我的工作浏览器是IE8,由于这是一个公司内部网站点,因此我不需要它与其他浏览器兼容,因为我们公司希望我们将IE专门用于与工作相关的任务。

请帮助:(另外,如果您可以推荐我正在做的替代品,请随时给他们:)

非常感谢,

POCH

2 个答案:

答案 0 :(得分:2)

您忘了在案件中加break;

case 0: 
  ...
  break; 
case 1: 
  ...
  break; 
...

答案 1 :(得分:0)

我已经忘记了使用jQuery已经很久了,但是在setAttribute之前尝试调用removeAttribute。