在 Razor 页面中编辑元素的类列表
我有一个 Razor 页面,在 HTML 中有一个按钮列表,我希望能够更改这些按钮上的类列表以突出显示当前选定的按钮。每个按钮看起来都是这样的:
<form asp-page-handler="Posts" method="post">
<button href="posts" class="nav-link text-white active" aria-current="page">
Posts
</button>
</form>
当单击它时,它将调用一个处理程序方法,我想向其中添加活动类并从前一个按钮中删除活动类。这在 javascript 中是微不足道的,也许在 C# 中是微不足道的,但经过一个小时的搜索,我找不到一种方法来做到这一点。
I have a Razor Page and in the HTML there's a list of buttons and I want to be able to change the class list on those buttons to highlight the currently selected one. Each button looks like so:
<form asp-page-handler="Posts" method="post">
<button href="posts" class="nav-link text-white active" aria-current="page">
Posts
</button>
</form>
When it is clicked it will call a handler method and I want to add the active class to it and remove the active class from the previous one. This would be trivial in javascript and maybe it is in c# but after an hour of searching, I can't find a way to do this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个在没有 js 的情况下将活动类添加到选定按钮的工作演示:
cshtml.cs:
Button.cs:
查看:
结果:
Here is a working demo to add the active class to selected button without js:
cshtml.cs:
Button.cs:
View:
result:
下面的解决方案使用jquery/js。
.nav-link
按钮。我使用数据页。对于其他按钮,您只需分配唯一的数据页即可;
data-page= "other name";
然后在各自的处理方法中,添加
ViewData["ActivePage"] = "other name";
因为脚本将包含在布局,它将激活所有页面。
The solution below uses jquery/js.
.nav-link
buttons. I used data-page.For the other buttons, you just need to assign unique data-page;
data-page= "other name";
Then in their respective handler methods, add
ViewData["ActivePage"] = "other name";
Since the script will be included in layout, it would activate for all the pages.
我使用 jquery/js 在这里编写一个简单的演示,以显示当单击按钮时,按钮将添加
active
类和粗体边框
以显示差异并从中删除 active 类上一个I use jquery/js write a simple demo here to show when click on the button, button will add
active
class andBold borders
to show the difference and remove the active class from the previous one