从后面的代码修改HTML标记级别CSS选择器

时间:2022-11-22 23:07:08

I want to be able to fully manipulate all CSS selectors (including classes and semantic tags) using the ASP.NET backend code. Like so:

我希望能够使用ASP.NET后端代码完全操作所有CSS选择器(包括类和语义标记)。像这样:

CSS:

CSS:

html {
    color: black;
}

.myClass {
    width: 100px;
}

Desired C#:

期望的C#:

protected void Page_Load(...) {
    // Some code & if logic ...
    html.Attributes.Add("style", "color: green")
    myClass.Attributes.Add("style", "width: 325px")
}

However, the C# aspect of it I do not know what to do to access semantic level tags. I am curious how to modify just the style classes from C# (i.e. do not mess with the ASP.NET / id's). This is for design aspects as CSS does not have certain logic behind it and I cannot make another "html" selector in CSS or C#.

但是,它的C#方面我不知道如何访问语义级别标签。我很好奇如何修改C#中的样式类(即不要乱用ASP.NET / id)。这是设计方面,因为CSS背后没有某些逻辑,我不能在CSS或C#中创建另一个“html”选择器。

EDIT:

编辑:

Class applied to a generic div element containing other elements. runat="server"

应用于包含其他元素的泛型div元素的类。 RUNAT = “服务器”

2 个解决方案

#1


2  

Try this CSS parser. I think you will be able to accomplish your task. If not, it is open source, you can use it as a guide.

试试这个CSS解析器。我想你将能够完成你的任务。如果没有,它是开源的,你可以用它作为指南。

This code below will find "url('/images/logo.png')"

以下代码将找到“url('/ images / logo.png')”

var parser = new Parser();
var stylesheet = parser.Parse(".someClass{color: red; background-image: url('/images/logo.png')");

var imageUrl = stylesheet.Rulesets
            .SelectMany(r => r.Declarations)
            .FirstOrDefault(d => d.Name.Equals("background-image", StringComparison.InvariantCultureIgnoreCase))
            .Term
            .ToString(); 

#2


0  

whatever controls you want to access in the code, should be runat="server" and with id.

无论你想在代码中访问什么控件,都应该是runat =“server”和id。

< div id="dv" runat="server">...your design in between...< /div>

from the backend , you can access it like this:

从后端,您可以像这样访问它:

dv.Attributes.Add("style", "color: green");

well, I suggest to have a css, and you can assign the class directly:

好吧,我建议有一个CSS,你可以直接分配这个类:

dv.Attributes.Add("class", "myClass");

or

要么

dv.cssClass ="myClass";

Note: if you are looking for different themes can be applied on the user selection refer this link https://msdn.microsoft.com/en-us/library/ms366514.aspx http://www.c-sharpcorner.com/UploadFile/8dd3df/themes-in-Asp-Net/

注意:如果您正在寻找可以在用户选择上应用的不同主题,请参阅此链接https://msdn.microsoft.com/en-us/library/ms366514.aspx http://www.c-sharpcorner.com/ UploadFile / 8dd3df /主题功能于ASP-网/

#1


2  

Try this CSS parser. I think you will be able to accomplish your task. If not, it is open source, you can use it as a guide.

试试这个CSS解析器。我想你将能够完成你的任务。如果没有,它是开源的,你可以用它作为指南。

This code below will find "url('/images/logo.png')"

以下代码将找到“url('/ images / logo.png')”

var parser = new Parser();
var stylesheet = parser.Parse(".someClass{color: red; background-image: url('/images/logo.png')");

var imageUrl = stylesheet.Rulesets
            .SelectMany(r => r.Declarations)
            .FirstOrDefault(d => d.Name.Equals("background-image", StringComparison.InvariantCultureIgnoreCase))
            .Term
            .ToString(); 

#2


0  

whatever controls you want to access in the code, should be runat="server" and with id.

无论你想在代码中访问什么控件,都应该是runat =“server”和id。

< div id="dv" runat="server">...your design in between...< /div>

from the backend , you can access it like this:

从后端,您可以像这样访问它:

dv.Attributes.Add("style", "color: green");

well, I suggest to have a css, and you can assign the class directly:

好吧,我建议有一个CSS,你可以直接分配这个类:

dv.Attributes.Add("class", "myClass");

or

要么

dv.cssClass ="myClass";

Note: if you are looking for different themes can be applied on the user selection refer this link https://msdn.microsoft.com/en-us/library/ms366514.aspx http://www.c-sharpcorner.com/UploadFile/8dd3df/themes-in-Asp-Net/

注意:如果您正在寻找可以在用户选择上应用的不同主题,请参阅此链接https://msdn.microsoft.com/en-us/library/ms366514.aspx http://www.c-sharpcorner.com/ UploadFile / 8dd3df /主题功能于ASP-网/