[英]How to change the highlight color of multiple (at least two) different span or div concurrently by hover or mouseover?
我正在處理有時具有彼此相關的不同部分的文本內容,我想讓用戶將鼠標懸停/鼠標懸停在任何相關部分上,並因此用相同的突出顯示部分同時突出顯示所有相關部分顏色。
假設在將內容生成為HTML之前,我確實(在服務器上)具有與哪些部分相關的所有信息,這是最快的方法。 即,我可以放入適當的CSS或javascript,以臨時方式(每頁)獲得所需的設置。
最快的方法是在您的身體或頂部容器元素上切換一個類,並以對其后代進行彩色/突出顯示等方式定義CSS。這比在關聯中的每個元素中添加/刪除類要快。
例如:如果所有相關元素都具有“ rel-1”類,則可以具有如下CSS定義:
body.rel-1 .rel-1 {
background-color: #ddd;
/* .. other styles */
}
然后將鼠標懸停在任何具有rel-1
類的元素上,即可在body / container元素中切換相同的類。
例如:
$('.rel-1').mouseover(function() {
$(body).addClass('rel-1');
});
$('.rel-1').mouseout(function() {
$(body).removeClass();
});
等等..
如果我理解您的問題,則可以使用jquery輕松完成。
首先,我將設置具有屬性的html元素。 我正在使用thisisfor
,您可以使用( 幾乎 )任何東西。 因此,如果可以在服務器端預先確定每個項目屬於哪個“組”,則可以使用您選擇的屬性對它們進行分類,然后再對任何“組”進行分類。
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>
然后,使用jquery,您可以設置一個懸停事件來檢查屬性,在本例中為thisisfor
屬性:
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) */
});
但是,是的,使用屬性的可能性是無限的...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.