简体   繁体   English

javascript cancelBubble 在 Firefox 上不起作用

[英]javascript cancelBubble doesn't work on firefox

the blow code work fine on IE, but doesn't work on firefox, why?打击代码在 IE 上工作正常,但在 Firefox 上不起作用,为什么? It's some problem on my code?我的代码有问题吗? How to fix it?如何解决?

<html>
<head>
<style>
body{font-family:Arial;font-size:12px;font-weight:normal;line-height:28px;}
.product_tips{
width:500px;
background:#f0f0f0;
border:1px solid #ccc;
padding:8px;
margin-top:3px;
}
span{cursor:pointer;}
</style>
<script type="text/javascript">
    function get(id){ 
        return document.getElementById(id);
    }
    function showTip(e){
        if(get("product_tips").style.display == "none"){
        get("product_tips").style.display = "block";
    } else{
        get("product_tips").style.display = "none";
    }
    stopBubble(e)
}
function stopBubble(e) {
    if (e){
     e.stopPropagation();
     }
    else{
     window.event.cancelBubble = true;
    }
}
document.onclick = function(){
        get("product_tips").style.display = "none";
    }
</script>
</head>
<body>
<div class="relative_">
<label><input type="text" name="#" value="" id="product_name" maxlength="6" /></label>&nbsp;&nbsp;<span onclick="showTip();">help ?</span>
                <div id="product_tips" class="product_tips" style="display:none;" onclick="stopBubble();">
                    <div class="product_inbox">
                        <p>content content content content content content content content content content content content content content content content content content content content </p>
                    </div>
                </div>
                </div>
</body>
<html>

the demo here: http://jsbin.com/ivosa3这里的演示: http : //jsbin.com/ivosa3

Try not setting the event handler in the attribute but instead set it in script.尽量不要在属性中设置事件处理程序,而是在脚本中设置它。 The following works in both IE and Firefox:以下适用于 IE 和 Firefox:

<html>
<head>
<style>
body{font-family:Arial;font-size:12px;font-weight:normal;line-height:28px;}
.product_tips{
width:500px;
background:#f0f0f0;
border:1px solid #ccc;
padding:8px;
margin-top:3px;
}
span{cursor:pointer;}
</style>
<script type="text/javascript">
  function get(id){
    return document.getElementById(id);
  }
  function showTip(e){
    if(get("product_tips").style.display == "none"){
    get("product_tips").style.display = "block";
  } else{
    get("product_tips").style.display = "none";
  }
    stopBubble(e)
}
function stopBubble(e) {
    if (e){
     e.stopPropagation();
   }
    else{

     window.event.cancelBubble = true;
  }
}
document.onclick = function(e){
    get("product_tips").style.display = "none";
  }
</script>
</head>
<body>
<div class="relative_">
<label><input type="text" name="#" value="" id="product_name" maxlength="6" /></label>&nbsp;&nbsp;<span id="help">help ?</span>
        <div id="product_tips" class="product_tips" style="display:none;">
          <div class="product_inbox">
            <p>content content content content content content content content content content content content content content content content content content content content </p>
          </div>
        </div>
        </div>
  <script type="text/javascript">
      get('help').onclick = showTip;
      get('product_tips').onclick = stopBubble;
    </script>
</body>
<html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM