【问题标题】:How to toggle a class in Blazor on button click?如何在单击按钮时切换 Blazor 中的类?
【发布时间】:2023-01-20 02:00:15
【问题描述】:
我目前有一个 onclick 事件可以像这样切换一个类:
....
<button id="@m.TargetCo.ButtonId" onclick="glyphChanger(this.id)" class="btn btn-default iconButton glyphicon glyphicon-chevron-right" ></button>
....
function glyphChanger(buttonID) {
$("#" + buttonID).toggleClass('glyphicon-chevron-right glyphicon-chevron-up');
}
【问题讨论】:
标签:
html
blazor
blazor-webassembly
【解决方案1】:
不需要javascript。您可以使用 Blazor 方式执行此操作。
<button type="button" class="@buttonCss" @onclick="ChangeButtonClass">Test</button>
@code {
private string buttonCss = "glyphicon-chevron-right";
private void ChangeButtonClass()
{
buttonCss = buttonCss == "glyphicon-chevron-right" ? "glyphicon-chevron-up" : "glyphicon-chevron-right";
}
}