简体   繁体   English

更改一个元素的样式而不更改另一个元素?

[英]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;
}

Here's a live example. 这是一个实时示例。

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>

Alt 1 替代项1

You could add a style attribute to that anchor: 您可以将style属性添加到该锚点:

<a href="" class="menu-link" style="color:#000;">home</a>

Alt 2 Alt 2

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。

Alt 3 Alt 3

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.

相关问题 在更改先前的输入而没有一步间隙后,我应该怎么做才能更改选择选项? 反应 - What should i do to change select options after changeing previous input without one step gap? react 在没有JavaScript的情况下更改元素样式 - Change element style without JavaScript CSS中是否可以通过单击另一个元素来更改元素的样式? - Is it possible in CSS to change the style of an element by clicking on an other? 将鼠标悬停到div即可更改其他元素的样式 - hover to div to change other element's style 将鼠标悬停在一个元素上会导致其他元素发生变化 - Hover on one element causes other element to change 更改任何元素的悬停样式而不更改父元素的样式 - Change the style on hover for any element without changing the parent's style 从另一个元素React上的mouseOver更新一个元素的样式 - Update style of one Element from mouseOver on other Element React 如何使用带有节点列表的Javascript更改其他元素的样式 - How to change style on other element using Javascript with nodelist 将鼠标悬停在其他页面元素上时更改点样式 - Change point style on hover over other onpage element setInterval 在没有 jquery 的 javascript 中逐个更改元素集的样式 - setInterval to change style of set of elements one by one in javascript without jquery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM