我想知道如果它可能使下面的代碼更簡潔:多個選擇器或多個功能 - 任何效率增益?
$('#americasTrigger').hover(
function() {
$('#americasImg').fadeIn()
},
function(){
$('#americasImg').fadeOut()
}
);
$('#europeTrigger').hover(
function() {
$('#europeImg').fadeIn();
},
function(){
$('#europeImg').fadeOut();
}
);
$('#middleEastTrigger').hover(
function() {
$('#middleEastImg').fadeIn();
},
function(){
$('#middleEastImg').fadeOut();
}
);
//More etc
國名保持每個相同的,與「觸發」或「圖」添加到末尾。這裏有很多重複,這表明我不會採用這種最佳方式。
我有想法圍繞:
- Crearting一個的情況下,或
- 不知怎的讓選擇被用於選擇,使其成爲一個字符串,分裂它捕捉到該國在使用和應用名稱對於嵌套的淡入/淡出功能,最後使用'Img'。
這是可能的還是我太過幻想?
編輯1:非常感謝所有的答覆,不發佈html的道歉,我把這個波紋管。 總之,我使用bg圖像(地球)上的圖像地圖作爲懸停觸發器來淡入/淡出我絕對定位的懸停圖像。
<div class="mapTub">
<img src="images/transparentPixel.png" class="mapCover" usemap="#worldMap" width="524px" height="273px"/>
<map name="worldMap" id="worldMap">
<area id="americasTrigger" shape="poly" coords="1,2,3" href="#americas" />
<area id="europeTrigger" shape="poly" coords="4,5,6" href="#europe" />
<area id="middleEastTrigger" shape="poly" coords="7,8,9" href="#middleEast" />
</map>
<img src="images/International_americas_dark.png" class="americas" id="americasImg" />
<img src="images/International_europe_dark.png" class="europe" id="europeImg" />
<img src="images/International_middleEast_dark.png" class="middleEast" id="middleEastImg" />
</div>
Reigel的答案似乎是要走的路在這裏,虐待嘗試一下回來報告,進一步的評論歡迎! :)
html代碼請... – Reigel 2010-07-30 06:24:29
是的,你的HTML看起來像一些例子會有所幫助。 – belugabob 2010-07-30 10:00:53