javascript()onclick hide元素 - 不能在firefox中工作

时间:2015-03-25 09:13:59

标签: javascript html

我有javascript,onclick显示和隐藏定义style.display = "inherit";的元素 并style.display = "none"; ..

'隐藏'按下ESC按钮时,该功能也在运行.. 在CHROME,OPERA等一切都很好..除了FIREFOX ..

    window.onload=function() {
    document.body.onkeyup = key_event;
    }

    function showDiv() {
        document.getElementById('PhotoFull').style.display = "inherit";
        event.stopPropagation();
     }

    function hideDiv() {
         var target = event.target || event.srcElement;
         if (event.currentTarget == target) {
                 document.getElementById('PhotoFull').style.display = "none";
         }
     }

    function key_event(e) {
        if (e.keyCode == 27) hideDiv();
        }

showDiv()有效,但hideDiv没有......

HTML:

 <div id="PhotoFull" style="position: absolute; width: 100%; height: 100%; background-color: rgba(0,0,0, 0.75); display:none; z-index: 999;" onclick="hideDiv()">
      <div style='width: 1000px'>
            SOME CONTENT
      </div>
 </div>

这是JSFIDDLE:http://jsfiddle.net/5e6ww93h/2/

感谢您的关注

2 个答案:

答案 0 :(得分:3)

语法上,您必须将事件obj传递给hideDiv函数

function hideDiv(event) {
     var target = event.target || event.srcElement;
     if (event.currentTarget == target) {
             document.getElementById('PhotoFull').style.display = "none";
     }
 }

function key_event(e) {
    if (e.keyCode == 27) hideDiv(e);
}

答案 1 :(得分:0)

尝试以下

  window.onload=function() {
    document.body.onkeyup = key_event;
    }

    function showDiv() {
        document.getElementById('PhotoFull').style.display = "inherit";
        event.stopPropagation();
     }

    function hideDiv(e) {

         var target = e.target || e.srcElement;

         if (e.currentTarget == target) {
                 document.getElementById('PhotoFull').style.display = "none";
         }
     }

    function key_event(e) {   
        if (e.keyCode == 27)
                       hideDiv(e);
               }

fiddler: - http://jsfiddle.net/5e6ww93h/4/