阻止冒泡
非IE下:event.stopPropagation();
IE下:event.cancelBubble=true;
<div id="aaa">
点击我
<div id="bbb">
点击我
</div>
</div>
<script>
var da=document.getElementById('aaa');
var xiao=document.getElementById('bbb');
da.onclick=function(e){
var ao=e||event;
ao.stopPropagation();//阻止事件冒泡,非IE下
ao.cancelBubble==true;//阻止事件冒泡,IE下
alert('大盒子');
}
xiao.onclick=function(e){
var ao=e||event;
ao.stopPropagation();//阻止事件冒泡,非IE下
ao.cancelBubble==true;//阻止事件冒泡,IE下
alert('小盒子');//点击小盒子,会依次弹出小盒子,大盒子,如果给body,HTML加点击事件,也会依次弹出,从里到外称为事件冒泡
}
</script>
<input type="button" value="点击按钮" id="btn">
<div id="zs"></div>
<script>
var btn=document.getElementById('btn');
var hz=document.getElementById('zs');
btn.onclick=function(e){
var bo=e||event;
bo.stopPropagation();
hz.style.display='block';
}
document.onclick=function(){
hz.style.display='none';
}
</script>
JS计时事件
计时器setInterval与clearInterval
<button onclick="func()">点击开始</button>
<button onclick="fund()">点击结束</button>
<script>
//点击开始按钮每隔一秒弹出一段话
//点击结束按钮停止计时器
var aa;
function func(){
aa=window.setInterval("alert('间隔一秒弹出内容');",3000);
}
function fund(){
window.clearInterval(aa);
}
</script>
计时器setTimeout与clearTimeout
<button onclick="funf()">点击开始</button>
<button onclick="fune()">点击结束</button>
<script>
//setTimeout等待几秒只执行一次,所以清除要在执行之前清除
var ff;
function funf(){
ff=setTimeout(function(){
alert('你好')
},3000)
}
function fune(){
clearTimeout(ff);
}
</script>
Comments | NOTHING