[英]change style of one element without changeing the other?
sorry the question doesnt make sense but i shall explain. 抱歉,这个问题没有道理,但我会解释。 I have two hyperlinks
我有两个超链接
<a href="" class="menu-link">home</a>
<a href="" class="menu-link">blog</a>
i want to apply this style 我想应用这种风格
<style>
.menu-link{color:#000;}
</style>
but only apply to <a href="" class="menu-link">home</a>
how can this be done other than actually changing the class like below 但仅适用于
<a href="" class="menu-link">home</a>
除了实际更改如下所示的类之外,该如何做?
<a href="" class="menu-link-1">home</a>
<a href="" class="menu-link-2">blog</a>
If you're unable to modify the markup to add an additional class, you can select an element it by its href
attribute, assuming it has one: 如果您无法修改标记以添加其他类,则可以通过其
href
属性选择一个元素,并假设它具有一个:
a.menu-link[href='home.html'] {
color: #000;
}
You can assign multiple class to a single element. 您可以将多个类分配给单个元素。
<a href="" class="menu-link home">home</a>
<a href="" class="menu-link">blog</a>
all you need is add a space 您只需要添加一个空间
<style>
.menu-link.home{color:#000;}
</style>
Assuming you can't / won't change the actual markup, try this (jQuery) 假设您不能/不会更改实际的标记,请尝试使用此(jQuery)
jQuery(function($) {
var home = $("a.menu-link:contains('home')");
// change style
home.css("color", "#000");
// add class
home.addClass("other-class");
// remove class
home.removeClass("menu-link");
// add / change attributes
home.attr("id", "some-id");
});
Maybe this solution will fit your needs. 也许此解决方案将满足您的需求。
<a href="" class="menu-link black">home</a>
<a href="" class="menu-link">blog</a>
<style>
.menu-link{}
.black {color:#000;}
</style>
You could add a style
attribute to that anchor: 您可以将
style
属性添加到该锚点:
<a href="" class="menu-link" style="color:#000;">home</a>
Or you could add an id
to that anchor: 或者,您可以向该锚添加
id
:
<a href="" class="menu-link" id="specialstyle">home</a>
, and replace .menu-link
with #specialstyle
in your css. ,并更换
.menu-link
与#specialstyle
在你的CSS。
Or you could give that anchor an additional class
: 或者,您可以给该锚定一个额外的
class
:
<a href="" class="menu-link menu-link-special">home</a>
, and replace .menu-link
with .menu-link-special
in your css. ,并更换
.menu-link
用.menu-link-special
在你的CSS。
You could add another class to menu-link-1
with the style you want: 您可以使用所需的样式向
menu-link-1
添加另一个类:
<style>a.home { color: #000; }</style>
<a href="" class="menu-link-1 home">home</a>
<a href="" class="menu-link-2">blog</a>
edit : obviously, if I read the question, I would have noticed that the author didn't want to change the classes.. 编辑 :显然,如果我读了这个问题,我会注意到作者不想更改类。
I am thinking that the 'home' link is the first one in these sequence so why not use 我认为“家庭”链接是这些顺序中的第一个链接,所以为什么不使用
.menu-link { color: <your_default_color>; }
.menu-link:first-child { color: #000; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.