<!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>无标题文档</title>
<style type="text/css">
.O{ color:#C00;}
.X{ color:#090;}
</style>
</head>
<body>
<div id="QA">
    <dl>
      <dt>第一题</dt>
      <dd class="right">第一题答案</dd>
      <dd>第一题答案</dd>
    </dl>
    <dl>
      <dt>第二题</dt>
      <dd class="right">第二题答案</dd>
    </dl>
    <dl>
      <dt>第三题</dt>
      <dd class="right">第三题答案</dd>
      <dd>第三题答案</dd>
    </dl>
</div>
<script type="text/javascript">
    var answera=document.getElementById("QA").getElementsByTagName("dd");
    
    for(var i=0;i<answera.length; i++){
        answera[i].onclick=function(){
            if(this.className=="right"){
                //给这个dd最近的dt加上class="O"
            }else{
                //给这个dd最近的dt加上class="X"
            }
        }
    }
</script>
</body>
</html>///////////////////////////////////////////////////if(this.className=="right"){
                //给这个dd最近的dt加上class="O"
            }else{
                //给这个dd最近的dt加上class="X"
            }这段要怎么写呢?求助。

解决方案 »

  1.   

    <script type="text/javascript">
    var answera=document.getElementById("QA").getElementsByTagName("dd");
    for (var i=0;i<answera.length; i++) {
    answera[i].onclick=function() {
       if(this.className=="right"){
    this.parentNode.getElementsByTagName('dt')[0].className = 'O';
       //给这个dd最近的dt加上class="O"
       }
    else {
    this.parentNode.getElementsByTagName('dt')[0].className = 'X';
    //给这个dd最近的dt加上class="X"
    }
    }
    }
    </script>