从 C# 中的 div 中删除类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10192919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 12:49:11  来源:igfitidea点击:

removing class from a div in c#

c#css

提问by neuDev33

I tried using the server-side .CssClass, but this isn't available for me to use. My div has runat="server"so I can access it, but I just cannot see .CssClassin the drop-down when i hit ".". Does anyone know how to get past this or any other solution that might work?

我尝试使用 server-side .CssClass,但这对我来说不可用。我的 div 有runat="server"所以我可以访问它,但是.CssClass当我点击“.”时我看不到下拉列表。有谁知道如何解决这个问题或任何其他可能有效的解决方案?

Ok, my bad. I just found out that this does not work for HTML controls, only for asp controls

好吧,我的错。我刚刚发现这不适用于 HTML 控件,仅适用于 asp 控件

采纳答案by Tim B James

If you are wanting to read the class name, you will need to use the code:

如果您想读取类名,则需要使用以下代码:

string className = Test.Attributes["class"].ToString();

If you are wanting to replace a specific class you will need to use the code:

如果要替换特定类,则需要使用以下代码:

Test.Attributes.Add("class", Test.Attributes["class"].ToString().Replace("spacerDiv", ""));

回答by huMpty duMpty

HTML
<div id="Test" runat="server"></div>

Code Behind

背后的代码

add =>       Test.Attributes.Add("class", "myClass")

remove =>    Test.Attributes.("class") = "";

回答by Somnath Kharat

This would be more specific:

这将更具体:

HTML

HTML

<div id="Test" runat="server"></div>

Servewr code:

服务器代码:

Test.Attributes.Remove("class");

回答by TGarrett

This is the same solution as Tim, but I removed the not needed toString method.

这与 Tim 的解决方案相同,但我删除了不需要的 toString 方法。

myDiv.Attributes.Add("class", myDiv.Attributes["class"].Replace("spacerDiv", ""));     

To add a class to a div is somewhat the same.

将类添加到 div 有点相同。

myDiv.Attributes.Add("class", "spacerDiv"); 

回答by Abhishek Kanrar

Remove css class

删除css类

myDiv.Attributes["class"]=" ";

myDiv.Attributes["class"]="";

回答by Kasim Husaini

A better approach to handle adding or removing class for any HtmlGenericControl would be to use the extension as provided below:

处理为任何 HtmlGenericControl 添加或删除类的更好方法是使用如下提供的扩展:

  public static void RemoveCssClass(this HtmlGenericControl controlInstance, string css)
    {
        var strCssClass = controlInstance.Attributes["class"];
        controlInstance.Attributes["class"] = string.Join(" ", strCssClass.Split(' ').Where(x => x != css).ToArray());
    }

    public static void AddCssClass(this HtmlGenericControl controlInstance, string css)
    {
        var strCssClass = controlInstance.Attributes["class"];
        controlInstance.Attributes["class"] = string.Join($" {css} ", strCssClass.Split(' ').ToArray());
    }

For WebControl the same code can be modified as below:

对于 WebControl,可以修改相同的代码如下:

public static void RemoveCssClass(this WebControl controlInstance, string css)
        {
            controlInstance.CssClass = string.Join(" ", controlInstance.CssClass.Split(' ').Where(x => x != css).ToArray());
        }

    public static void AddCssClass(this WebControl controlInstance, string css)
    {
        controlInstance.CssClass = string.Join($" {css} ", controlInstance.CssClass.Split(' ').ToArray());
    }