[英]can i read the content of a css class runtime in code behind?
i have a page with a .css file linked to it. 我有一个链接到它的.css文件的页面。 Let's say there is this content in it: 假设其中包含以下内容:
.wrapper {
color:red;
}
is there a way to read the value of the .wrapper element runtime in the code behind? 有没有办法在后面的代码中读取.wrapper元素运行时的值?
the problem is that runtime a different stylesheet is linked so the .wrapper is always available, but the content is always different. 问题是运行时链接了不同的样式表,因此.wrapper始终可用,但内容总是不同的。
what i want to accomplish is to get the value (in this case: color:red;) from the currently attached stylesheet. 我想要完成的是从当前附加的样式表中获取值(在本例中:color:red;)。
Is that possible? 那可能吗?
EDIT: is it possible client side, then i can put it in a hidden field somehow 编辑:它可能是客户端,然后我可以把它放在一个隐藏的领域
I'm assuming that you will have the path (physical or virtual) to the CSS file you are linking to. 我假设您将链接到CSS文件的路径(物理或虚拟)。
If it is a file on the webserver, you can load this into a text file, or if it is on a remote server you can use an XMLHTTPRequest to go and get it. 如果它是Web服务器上的文件,您可以将其加载到文本文件中,或者如果它位于远程服务器上,您可以使用XMLHTTPRequest来获取它。
Once you have the body of the file loaded, the following reg-ex will parse the file, detect the wrapper class and pluck out the value of the color key into the first group of the match. 一旦你加载了文件的主体,下面的reg-ex将解析文件,检测包装类并将颜色键的值拉出到匹配的第一组中。
\.wrapper[\s|\S]*color\s*:\s*([^;]*);
Some sample code 一些示例代码
var inputfileReader = new StreamReader("path to my css file");
string inputLine = inputfileReader.ReadToEnd() ;
inputfileReader.Close();
Regex cssParser = new Regex(@"\.wrapper[\s|\S]*color\s*:\s*([^;]*);");
string myCssClass = string.Empty; ;
if (cssParser.Match(inputLine).Groups[1] != null)
myCssClass = cssParser.Match(inputLine).Groups[1].Value;
<script type="text/javascript">
window.getComputedStyle = window.getComputedStyle || function (element) { return element.currentStyle; };
window.onload = function () {
var dummy = document.createElement("span");
dummy.className = "wrapper";
document.getElementsByTagName("body")[0].appendChild(dummy);
document.getElementById("<%= WrapperColorHiddenField.ClientID %>").value = getComputedStyle(dummy).color;
document.getElementsByTagName("body")[0].removeChild(dummy);
}
</script>
The drawback of that solution is that you need to parse returned value: in IE red
value returned whereas in Chrome returned rgb(255,0,0)
该解决方案的缺点是您需要解析返回的值:在IE中返回red
值,而在Chrome中返回rgb(255,0,0)
This is tricky 这很棘手
At client side
its possible, 在client side
它可能,
Add a hidden element to page & assign the class and then you can easily get css properties of the DOM element using jquery. 添加隐藏元素到页面并分配类,然后您可以使用jquery轻松获取DOM元素的css属性。
var testElement = $("<div></div>");
testElement.addClass("wrapper");
testElement.css("display", "none");
$("body").append(testElement);
alert(testElement.css('color'));
At server side
if you want to get the values from css file, 在server side
如果要从css文件中获取值,
check this http://www.codeproject.com/KB/recipes/CSSParser.aspx 请查看http://www.codeproject.com/KB/recipes/CSSParser.aspx
Regards, NJ 此致,新泽西州
At client side playing with CSS classes is always possible. 在客户端,总是可以使用CSS类。 You can accesss any class on the client side. 您可以访问客户端的任何类。 Also you can add a hidden field or access it. 您还可以添加隐藏字段或访问它。 Use jquery: 使用jquery:
$("#<%=ServerSideId.ClientID%>").addClass( className );
If you can be more clearer with the requirements i can provide a solution. 如果您能够更清楚地了解我可以提供解决方案的要求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.