简体   繁体   English

使用MVC和Razor将css类放在div中

[英]Place a css class in a div using MVC and Razor

I have used following code to add css class: 我使用以下代码添加css类:

<div id="123" class="tab-pane @{ if (some_condition) { Html.Raw("active"); }; } ">

But it did not work. 但它没有用。

I hope for this result: 我希望这个结果:

<div id="123" class="tab-pane active">

@MichaelPerrenoud's answer is close. @ MichaelPerrenoud的答案很接近。 You need to wrap the whole conditional in parenthesis. 您需要将整个条件包装在括号中。

Using @() tells razor to output a string. 使用@()告诉razor输出一个字符串。 So 所以

<div id="123" class='tab-pane @(condition ? "active" : "")'>

我相信你可能想要这样做:

<div id="123" class='tab-pane @some_condition ? "active" : ""'>

you can add javascript code 你可以添加javascript代码

    <script>
       @if(true)
       $('#123').addClass("active");
    </script>

这应该这样做:

<div id="123" class="tab-pane @if (some_condition) { <text>active</text>  }">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM