2015-10-19 91 views
-1

我在嘗試創建非點擊型div時遇到了問題。stopPropagation()停止傳播錯誤

是的,它的繁殖停止,但停​​止什麼不應該停止,也不會停止我應該停止

JS

//screen has set stopPropagation 

$("#smokeScreen").click(function(){ 
    event.stopPropagation(); 
}) 

HTML

<div id="content"> 
    <!-- lots of stuff that has to not be clickable while "smokeScreen is visible" --> 
    <!-- lots of stuff that has to not be clickable while "smokeScreen is visible" --> 
    <div id="smokeScreen"> 
     <!-- covers whole viewport --> 

     <div id="form"><!-- another form stuff here --></div> 
     <!-- covers whole viewport --> 
    </div> 

    <!-- lots of stuff that has to not be clickable while "smokeScreen is visible" --> 
    <!-- lots of stuff that has to not be clickable while "smokeScreen is visible" --> 
</div> 

有趣的是,這段代碼讓所有的東西都可以點擊,除了形式...

那麼請,任何提示?

和指針事件:無;在CSS是不是一個解決方案,出於某種原因,它不工作爲簡單的CSS,我還沒有想到爲什麼,但我需要保持與舊瀏覽器的兼容性,這是非常新的聲明。

+2

'event'沒有定義 –

+0

@PranavCBalan雅,在FF –

+0

提供[MCVE](http://stackoverflow.com/help/MCVE)。你綁定了任何「形式」後代事件還是什麼? –

回答

1

所以,你看起來在這裏丟失的是stopPropagation()防止事件被冒泡到父元素。

不會阻止這些元素或任何兄弟元素被點擊。

你需要做的是放置一個覆蓋整個屏幕的元素,然後將z-index屬性的形式設置爲比該元素更高的值,以便它可見(然後應該不需要調用stopPropagation())。

使用你的代碼,內聯樣式,這裏有一個例子:

<div id="content"> 
    <div id='smoke_screen' style='z-index: 1000; position: fixed; top: 0; left: 0; bottom: 0; right: 0' /> 
    <!-- lots of stuff that has to not be clickable while "smokeScreen is visible" --> 
    <!-- lots of stuff that has to not be clickable while "smokeScreen is visible" --> 
    <div id="smokeScreen"> 
     <!-- covers whole viewport --> 
     <div id="form" style='z-index: 1001'><!-- another form stuff here --></div> 
     <!-- covers whole viewport --> 
    </div> 

    <!-- lots of stuff that has to not be clickable while "smokeScreen is visible" --> 
    <!-- lots of stuff that has to not be clickable while "smokeScreen is visible" --> 
</div>