我有一個圖層作爲背景,另一個坐在背景圖層上的圖層(圖標)都具有不同的功能。停止點擊圖層
當我點擊圖標時,圖標的功能起作用,它也通過背景層工作。這不是預期! 如何讓點擊只能在圖標上工作,不會通過背景圖層?謝謝。
HTML
<div class="redBG">
<div class="yellowIcon"></div>
</div>
CSS
.redBG{
background-color:red;
width: 300px;
height:30px;
z-index: 1;
}
.grayBG{
background-color: gray;
}
.yellowIcon{
background-color:yellow;
width: 20px;
height:20px;
cursor: pointer;
z-index:20;
}
.greenBG{background-color:green}
JS
$('.redBG').click(
function(){
$(this).toggleClass('grayBG');
});
$('.yellowIcon').click(
function(){
$(this).toggleClass('greenBG');
}
);
謝謝,Matthias非常感謝您的幫助!它按預期工作。你可以在這個函數中解釋'e'嗎?謝謝。 – 2015-02-06 01:33:09
@abcidd很高興我能夠幫助你。我剛剛在函數中更新了我的答案,並解釋了函數中的'e',因爲將它作爲註釋發佈太長。 – 2015-02-06 20:47:01
非常感謝,Matthias!你的解釋非常清楚直接。 – 2015-02-09 14:01:01