我努力改變文本的顏色,當它被點擊但沒有成功。 question
有一個標籤,四個標籤options
,一個標籤correct answer
和一個標籤explanation
。如何在使用jquery點擊文本時更改文本的顏色?
我試圖當任何option
用戶點擊,那麼它應該與correct answer
匹配,並更改選項,也就是文本的顏色時,答案是正確的文字顏色應該變成綠色,否則顏色應開啓變紅。
但是,當我點擊任何option
它只會變成紅色。正確的option
應該變成綠色,但它變成了紅色。我找不出原因。 看看我的代碼。告訴我我錯在哪裏以及解決方案是什麼。
的.aspx: -
<%@ Page Title="" Language="C#" MasterPageFile="~/Student/StudentPage.master" AutoEventWireup="true" CodeFile="studpractice.aspx.cs" Inherits="Student_studpractice" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<script type="text/javascript">
$(function() {
$(".optionclass").click(function() {
var $thisoption = $(this);
var $corrans = $(".correctans");
if ($thisoption.text() == $corrans.text()) {
$thisoption.css("color", "green");
} else {
$thisoption.css("color", "red");
}
});
});
</script>
<div>
<div id="tabs">
<ul>
<li><a href="#tabs-1">Reasoning</a></li>
<li><a href="#tabs-2">Quantitative Aptitude</a></li>
<li><a href="#tabs-3">English</a></li>
<li><a href="#tabs-4">Mathematics</a></li>
<li><a href="#tabs-5">Computer Concepts</a></li>
</ul>
<div id="tabs-1">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" width="100%">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Panel ID="Panel1" runat="server">
<asp:Label ID="Label1" runat="server" Text='<%#Eval("Question") %>'></asp:Label>
<br />
<br />
<br />
<span>A-</span><a href="javascript:void(0);" style="text-decoration:none;"> <asp:Label class="optionclass" ID="Label2" runat="server" Text='<%#Eval("Option1")%>' ></asp:Label></a>
<br />
<br />
<span>B-</span><a href="javascript:void(0);" style="text-decoration:none;"> <asp:Label class="optionclass" ID="Label3" runat="server" Text='<%#Eval("Option2")%>'></asp:Label></a>
<br />
<br />
<span>C-</span><a href="javascript:void(0);" style="text-decoration:none;"> <asp:Label class="optionclass" ID="Label4" runat="server" Text='<%#Eval("Option3")%>'></asp:Label></a>
<br />
<br />
<span>D-</span><a href="javascript:void(0);" style="text-decoration:none;"> <asp:Label class="optionclass" ID="Label5" runat="server" Text='<%#Eval("Option4")%>'></asp:Label></a>
<br />
<br />
<asp:Button class="panelButton" runat="server" Text="Show Answer" ClientIDMode="Static" />
<br />
<asp:Panel ID="anspanel" class="AnswerPanel" runat="server" ClientIDMode="Static">
<span>Correct Answer is :-</span><asp:Label class="correctans" ID="Label6" runat="server" Text='<%#Eval("CorrectAns")%>'></asp:Label>
<br />
<br />
<asp:Label ID="Label7" runat="server" Text='<%#Eval("Explanation")%>'></asp:Label>
</asp:Panel>
</asp:Panel>
<br />
<br />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
<div id="tabs-2">
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Panel ID="Panel1" runat="server">
<asp:Label ID="Label1" runat="server" Text='<%#Eval("Question") %>'></asp:Label>
<br />
<br />
<br />
<span>A-</span><a href="javascript:void(0);" style="text-decoration:none;"> <asp:Label class="optionclass" ID="Label2" runat="server" Text='<%#Eval("Option1")%>'></asp:Label></a>
<br />
<br />
<span>B-</span><a href="javascript:void(0);" style="text-decoration:none;"> <asp:Label class="optionclass" ID="Label3" runat="server" Text='<%#Eval("Option2")%>'></asp:Label></a>
<br />
<br />
<span>C-</span><a href="javascript:void(0);" style="text-decoration:none;"> <asp:Label class="optionclass" ID="Label4" runat="server" Text='<%#Eval("Option3")%>'></asp:Label></a>
<br />
<br />
<span>D-</span><a href="javascript:void(0);" style="text-decoration:none;"> <asp:Label class="optionclass" ID="Label5" runat="server" Text='<%#Eval("Option4")%>'></asp:Label></a>
<br />
<br />
<asp:Button class="panelButton" runat="server" Text="Show Answer" ClientIDMode="Static" />
<br />
<asp:Panel ID="anspanel" class="AnswerPanel" runat="server" ClientIDMode="Static">
<span>Correct Answer is :-</span><asp:Label class="correctans" ID="Label6" runat="server" Text='<%#Eval("CorrectAns")%>'></asp:Label>
<br />
<br />
<asp:Label ID="Label7" runat="server" Text='<%#Eval("Explanation")%>'></asp:Label>
</asp:Panel>
</asp:Panel>
<br />
<br />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
<div id="tabs-3">
Tab 3 Content
</div>
<div id="tabs-4">
Tab 4 Content
</div>
<div id="tabs-5">
Tab 5 Content
</div>
</div>
<input type="button" id="btnPrevious" value="Previous" style = "display:none"/>
<input type="button" id="btnNext" value="Next" />
</div>
</asp:Content>
可以請你把你的html代碼這裏。 –
你首先必須知道答案是否正確。然後改變顏色,這不應該很難。 – Ionut
只選擇.correctans不是全部,它顯然將它們全部設置爲紅色。 –