我正在尝试从Blazor的后台代码中操作CSS类,但不论怎么尝试都没有成功。
private async Task<int> SetAnswerColorAsync()
{
cssClass = isCorrect ? "correct" : "incorrect";
if (isCorrect)
{
await js.InvokeVoidAsync("appendToElement", $"{OptionId}", cssClass, "Correct Message");
}
else
{
await js.InvokeVoidAsync("appendToElement", $"{OptionId}", cssClass, "In Correct");
}
return 1;
}
如图所示,每个选项都有一个option_Number
的ID:
<div class="container">
<div class="quiz">
<!--...-->
<p id="feedbackMessage">Test</p>
<!--...-->
<div class="app-title">Quizzing!</div>
<div class="quiz-header">How much is 1+1?</div>
<!--!-->
<div class="quiz-body">
<!--!-->
<div class="option" id="option_0" b-te071097yv=""></div>
<!--!-->
<div class="option" id="option_1" b-te071097yv=""></div>
<!--!-->
<div class="option" id="option_2" b-te071097yv=""></div>
<!--!-->
<div class="option" id="option_3" b-te071097yv=""></div>
</div>
<button>Submit Quiz</button>
</div>
</div>
当选择正确答案时,我希望将其背景颜色更改为在站点CSS中定义的不同颜色。
.option {
background-color: #fff;
border-radius: 5px;
padding: 10px;
margin-bottom: 8px;
cursor: pointer;
box-shadow: -0.3px 0.3px 3px rgba(0,0,0,0.3);
}
.incorrect {
background-color: red;
border-radius: 5px;
padding: 10px;
margin-bottom: 8px;
cursor: pointer;
box-shadow: -0.3px 0.3px 3px rgba(0,0,0,0.3);
}
.correct {
background-color: green;
border-radius: 5px;
padding: 10px;
margin-bottom: 8px;
cursor: pointer;
box-shadow: -0.3px 0.3px 3px rgba(0,0,0,0.3);
}
.option:hover {
background-color: #28a745;
color: #fff;
border-radius: 3px;
padding: 10px;
}
该函数通过onclick事件调用:
protected async void SelectOption()
{
var test = OptionId;
GetOptionStyle(Option);
await SetAnswerColorAsync();
await OnOptionSelected.InvokeAsync(Option);
}
HTML部分:
<div class="option" id="@OptionId" @onclick="SelectOption">@Option</div>