启用时更改表格tr(th td)背景色

时间:2018-11-19 07:50:20

标签: jquery toggle

我有表tr并将其连接以进行切换。没问题。当我单击它时,将打开另一个tr。例如,当我单击问题tr时,它将显示答案tr。但是当答案打开时。我想更改背景问题。但是我不知道如何插入当前的Jquery中?

有什么主意吗?

jQuery

$(document).ready(function(){
    $("#open1").click(function(){
        $("#tr-answer1").toggle();
    });
});

HTML

<!-- Question! -->
                <tr class="question1">
                    <th><img src="img/question.png" class="Question"></th>
                    <td><span class="button-text">
                            sdfsdfsdfsdfsdf Some text
                        </span>
                        <span class="open-button-big">
                            <img src="img/open-button-big.png" class="open-button-big-class" id="open1">
                        </span>
                    </td>
                </tr>

                <!-- Answer Right Below! -->
                <tr id="tr-answer1">
                    <th><img src="img/answer.png" class="Question"></th>
                    <td>
                            Some text here too Some text here too Some text here too
                    </td>
                </tr>

我想在打开时将question1的背景更改为白色#a0d6e1。 非常感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

使用toggleClass代替toggle

id q1中添加quation tr并使用id切换类question1

$(document).ready(function() {
  $("#open1").click(function() {
    $("#q1").toggleClass('question1');

  });
});
.question1 {
  background-color: #a0d6e1;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table border="1">
  <tr class="question1" id="q1">
    <th><img src="img/question.png" class="Question"></th>
    <td><span class="button-text">
                            sdfsdfsdfsdfsdf Some text
                        </span>
      <span class="open-button-big">
                            <img src="img/open-button-big.png" class="open-button-big-class" id="open1">
                        </span>
    </td>
  </tr>

  <!-- Answer Right Below! -->
  <tr id="tr-answer1">
    <th><img src="img/answer.png" class="Question"></th>
    <td>
      Some text here too Some text here too Some text here too
    </td>
  </tr>
</table>