搜索
javascript - js 标题不知怎么描述
天蓬老师
天蓬老师 2017-04-10 16:24:49
[JavaScript讨论组]

怎么样才能点击h1标签的123456 不弹出"p"

<p style="border:1px solid red;width:100px;height:100px;" onclick="alert('p')">
    <h1 onclick="alert(123456)">
        123456
    <h1>
</p>
天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

全部回复(3)
高洛峰
         <p id="outer" style="border:1px solid red;width:100px;height:100px;">
            <h1 id="inner">123456<h1>
         </p>
        
        document.getElementById('outer').addEventListener('click',function(){
          alert('p');
        })
        document.getElementById('inner').addEventListener('click',function(e){
          //阻止冒泡
          e.stopPropagation();
          alert(123456);
        })
      stopPropagation()方法 http://www.w3school.com.cn/jsref/event_stoppropagation.asp
迷茫

这其实是个js冒泡。现在你要做的是阻止冒泡事件。

<p class="parent">
    <h1 class="child">孩子</h1>
</p>

$('parent').click(function(){
  alert(1);
})
 $('.parent .child').click(function(event){
  event.stopPropagation();//阻止冒泡事件
  alert(2);
})
怪我咯

这没办法吧,你把函数写在html里面了,你要是写在 script 里面的话,可以在 h1 绑定的那个函数里面最后加 e.preventDefault(), (e是传入函数的一个事件对象),或者 加 return false。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号