Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
293 views
in Technique[技术] by (71.8m points)

javascript - 如何使用内联onclick属性停止事件传播?(How to stop event propagation with inline onclick attribute?)

Consider the following:(考虑以下:)

<div onclick="alert('you clicked the header')" class="header"> <span onclick="alert('you clicked inside the header');">something inside the header</span> </div> How can I make it so that when the user clicks the span, it does not fire the div 's click event?(我怎样才能使它在用户单击跨度时不会触发div的click事件?)   ask by Sam translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use event.stopPropagation() .(使用event.stopPropagation() 。)

<span onclick="event.stopPropagation(); alert('you clicked inside the header');">something inside the header</span> For IE: window.event.cancelBubble = true(对于IE: window.event.cancelBubble = true) <span onclick="window.event.cancelBubble = true; alert('you clicked inside the header');">something inside the header</span>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...