简体   繁体   English

将CSS类添加到客户端超链接上的背后代码

[英]Adding CSS Class to client side hyperlink on code behind

I have a list of links : 我有一个链接列表:

<ul>
    <li><a href="#">Aktuel</a></li>
    <li><a href="#">Business</a></li>
    <li><a href="#">Common</a></li>
    <li><a href="#">Extras</a></li>
</ul>

I have to add a css class to one of the links. 我必须将css类添加到链接之一。 I will have to do it in codebehind with C#. 我将不得不在C#的代码隐藏中做到这一点。 The result should be something like this : 结果应该是这样的:

....
<li><a href="#" class="active">Business</a></li>
....

On which life cycle event should I do it and how? 我应该在哪个生命周期事件上执行该操作,以及如何执行?

Note : I cant change the html design. 注意:我无法更改html设计。

Thanx in advance. 提前感谢。

You can register a ClientScript.RegisterStartupScript on the CodeBehind, to run a jQuery script and do the job. 您可以在CodeBehind上注册ClientScript.RegisterStartupScript,以运行jQuery脚本并完成此工作。

Complete solution, including the ASP.NET bit: 完整的解决方案,包括ASP.NET位:

string jsScript = "$(function () { $(\"a:contains('Aktuel')\").addClass(\"active\"); });";

ClientScript.RegisterStartupScript(typeof(Page), "anythingYouFancy", jsScript, true);

You can check it working here (not the ASP.NET part tough): 您可以在这里检查它的工作情况(不是ASP.NET的难点):

http://jsfiddle.net/JwkAe/ http://jsfiddle.net/JwkAe/

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

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