简体   繁体   English

如何通过悬停或鼠标悬停同时更改多个(至少两个)不同跨度或div的突出显示颜色?

[英]How to change the highlight color of multiple (at least two) different span or div concurrently by hover or mouseover?

I'm working on text content that sometimes has different parts being related to each other, and I'd like to let user hover/mouseover any of the related parts, and accordingly highlight all the related parts at the same time with the same highlight color. 我正在处理有时具有彼此相关的不同部分的文本内容,我想让用户将鼠标悬停/鼠标悬停在任何相关部分上,并因此用相同的突出显示部分同时突出显示所有相关部分颜色。

What's the quickest way to do this, assuming that I do have all the information (on the server) regarding which parts are related, prior to generating the content into HTML. 假设在将内容生成为HTML之前,我确实(在服务器上)具有与哪些部分相关的所有信息,这是最快的方法。 Ie I can put in the proper css or javascript to get the needed setup in ad hoc, per page basis. 即,我可以放入适当的CSS或javascript,以临时方式(每页)获得所需的设置。

The fastest way would be toggling a class on your body or a top container element and defining the CSS in such a way that it descendants are corrected colored/highlighted etc. This will be faster than adding/removing classes from each element in the association. 最快的方法是在您的身体或顶部容器元素上切换一个类,并以对其后代进行彩色/突出显示等方式定义CSS。这比在关联中的每个元素中添加/删除类要快。

For example: if all related elements have the class "rel-1" then you can have a CSS definition like: 例如:如果所有相关元素都具有“ rel-1”类,则可以具有如下CSS定义:

body.rel-1 .rel-1 {
    background-color: #ddd;
    /* .. other styles */
}

And on hover over any element with class rel-1 , toggle the same class in the body/container element. 然后将鼠标悬停在任何具有rel-1类的元素上,即可在body / container元素中切换相同的类。

For example: 例如:

$('.rel-1').mouseover(function() {
    $(body).addClass('rel-1');
});

$('.rel-1').mouseout(function() {
    $(body).removeClass();
});

and so on.. 等等..

If I understand your question, this can be done using jquery pretty easily. 如果我理解您的问题,则可以使用jquery轻松完成。

First, I'd set up html elements with attributes. 首先,我将设置具有属性的html元素。 I'm using thisisfor , you can use ( almost ) anything. 我正在使用thisisfor ,您可以使用( 几乎 )任何东西。 So if you can predetermine which 'group' each item belongs to on the server side, you can classify them with an attribute of your choice, and then whatever 'group'. 因此,如果可以在服务器端预先确定每个项目属于哪个“组”,则可以使用您选择的属性对它们进行分类,然后再对任何“组”进行分类。

html html

<div class="hoverme" thisisfor="group3">What group are we in?</div>
<div class="hoverme" thisisfor="group3">What group are we in?</div>
<div class="hoverme" thisisfor="group2">What group are we in?</div>
<div class="hoverme" thisisfor="group4">What group are we in?</div>

Then, using jquery, you can set up a hover event that checks an attribute, the thisisfor attribute in our case: 然后,使用jquery,您可以设置一个悬停事件来检查属性,在本例中为thisisfor属性:

jquery jQuery的

$("[thisisfor]").hover(function(){ /* anything with 'thisisfor' attribute */

    var group = $(this).attr('thisisfor');  /* store its value as variable 'group' */

    $("[thisisfor="+ group +"]").html(group);  /* all with this group that was hovered....change the text (or whatever your plan is) */

});​

See my example 见我的例子

But, yea, the possibilities are endless using attributes... 但是,是的,使用属性的可能性是无限的...

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

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