简体   繁体   English

将鼠标悬停在一个跨度上,并通过添加和删除类来更改该跨度和另一跨度的背景色

[英]Hover over one span and change background color of that span plus another one by adding and removing a class

<span id="english">Yes</span>
<span id="spanish">Sí</span>

How can I hover over any of these and change background color to yellow on both. 我如何将鼠标悬停在其中任何一个上并将两个背景颜色都更改为黄色。 I can't use onmouseout() because the background color changes dynamically due to other scripts. 我不能使用onmouseout(),因为背景颜色会由于其他脚本而动态变化。 I'm aware that I can add a class skipping the use of jQuery -although it's a valid choice if all else fails- by using something like: 我知道我可以添加一个类跳过jQuery的使用-尽管如果其他所有方法都失败,这是一个有效的选择,则可以使用类似以下方法:

document.getElementById(id).className += " yellow";

and the css would be: 和CSS将是:

.yellow {
background-color: yellow
}

My previous solution that included onmouseout() was: 我以前的包括onmouseout()的解决方案是:

function chbg(color, id1, id2) {
document.getElementById(id1).style.backgroundColor = color;
document.getElementById(id2).style.backgroundColor = color;
}

and the HTML: 和HTML:

<span id="english" onmouseover="chbg('yellow', 'english', 'spanish')"      onmouseout="chbg('white','english', 'spanish')">Yes</span>
<span id="spanish" onmouseover="chbg('yellow', 'english', 'spanish')" onmouseout="chbg('white','english', 'spanish')">Sí</span>

Use JQuery hover function instead. 请改用JQuery hover功能。

Try this: 尝试这个:

$(document).ready(function(){
    $("span").hover(
    function(){
        $('span').css('background', 'yellow');
    }, 
    function(){
        $('span').css('background', 'white');
    });
});

JSFiddle Demo #1 (With Class) JSFiddle演示#1(带类)

JSFiddle Demo #2 (Without Class) JSFiddle演示#2(无课程)

UPDATE #1 更新#1

Use toggleClass() function instead. 请改用toggleClass()函数。

$(document).ready(function(){
    $("span").hover(function(){
        $('span').toggleClass('highlight');
    });
});

JSFiddle Demo JSFiddle演示

UPDATE #2 更新#2

Assign a class to all the span that needs to be highlighted. 将一个class分配给所有需要突出显示的span For example: class="highlight" . 例如: class="highlight" Using toggleClass() to toggle a class from CSS will add another class now. 使用toggleClass()CSS切换一个class将立即添加另一个类。 This way only span with .highlight change color. 这种方式仅span .highlight更改颜色。

JSFiddle Demo JSFiddle演示

This can be done with only CSS, by adjusting your HTML a bit: 这可以仅通过CSS来完成,只需稍微调整一下HTML:

<span id="bghover">
    <span>Yes</span>
    <span>Sí</span>
</span>

And for the CSS: 对于CSS:

#bghover span
{
    background-color: white;
}
#bghover:hover span
{
    background-color: yellow;
}

So you wrap the two spans into a span or div with id bghover , which is only used as a trigger for CSS :hover . 因此,您可以将两个span打包为id为bghover的span或div,后者仅用作CSS :hover的触发器。 If there's no hover, all spans within #bghover are white, if there is a hover (similar to onmouseover), all spans within #bghover are white. 如果没有悬停,则#bghover内的所有跨度均为白色,如果有悬停(类似于onmouseover),则#bghover内的所有跨度均为白色。

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

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