Asp.Net-MVC:如何使用 Css 设置 <%=Html.ActionLink()%> 样式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2244349/
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-29 02:10:43  来源:igfitidea点击:

Asp.Net-MVC: How to style <%=Html.ActionLink()%> with Css?

htmlasp.net-mvccss-selectors

提问by Richard77

Here's a piece of my HTML code

这是我的一段 HTML 代码

<div id = "mydiv">
    <% = Html.ActionLink("Some Text","SomeAction")%>
</div>

I'd like to style it in white so that it doesn't conflict with the background, which is also blue. So I did this:

我想将它设置为白色,这样它就不会与背景冲突,背景也是蓝色。所以我这样做了:

#mydiv {background-color:blue;} 
#mydiv a:link { color:white}

But, it doesn't work -- the color it's still blue. How can I do it? Maybe, I just did not write well the selectors.

但是,它不起作用——它的颜色仍然是蓝色。我该怎么做?也许,我只是没有写好选择器。

Thanks for helping.

谢谢你的帮助。

采纳答案by Darin Dimitrov

#mydiv a { color:white; }

回答by Richard Szalay

Remove the :linksuffix and you should be fine:

删除:link后缀,你应该没问题:

#mydiv { background-color:blue; }
#mydiv a { color:white; }

Alternatively you can add a class name to the link:

或者,您可以向链接添加类名:

<div id="mydiv"> 
    <%= Html.ActionLink("Some Text", "SomeAction", 
            new { @class = "class-name" }) %> 
</div> 

回答by luke

Maybe

也许

<%=Html.ActionLink("Text","Act","Ctrl",new {@style="color:white;"}) %>

回答by Dkong

Mine is like Luke's but I have a null in there (I'm using MVC2)

我的就像卢克的,但我在那里有一个空值(我使用的是 MVC2)

<%=Html.ActionLink("Text","Act","Ctrl",new {@style="color:white;"}) %>

回答by Rohit

In my case this one worked

在我的情况下,这个有效

HTML.ActionLink("LinkLabel", "ActionName", "Controller", null, 
               new {@class="btn btn-primary pull-right"})

If I don't use null above the proper controller action i.e. Controller.ActionNamemethod was not called. Instead something like currentController/Length==4something like this was called.

如果我不在正确的控制器操作之上使用 null,即没有调用Controller.ActionName方法。相反,像currentController/Length==4这样的东西被调用了。

回答by Fermin

Try removing the :link and just having

尝试删除 :link 并只拥有

#mydiv a { color:white}

this should colour the link white.

这应该将链接着色为白色。

I'd recommend using the Firebugplugin for firefox also, this allows you to change the stylesheet and see instant changes as well as see which styles are being applied to each element, which styles are being 'overuled' by other styles etc.

我还建议为 firefox使用Firebug插件,这允许您更改样式表并查看即时更改以及查看哪些样式应用于每个元素,哪些样式被其他样式“否决”等。

回答by JonoW

Try:

尝试:

#mydiv a { color:white}

Also, try remove the whitespace around your Id attribute (just in case): ->

另外,尝试删除 Id 属性周围的空格(以防万一):->