インライン onclick 属性を使用してイベントの伝播を停止するにはどうすればよいでしょうか? 質問する

インライン onclick 属性を使用してイベントの伝播を停止するにはどうすればよいでしょうか? 質問する

次の点を考慮してください。

<div onclick="alert('you clicked the header')" class="header">
  <span onclick="alert('you clicked inside the header');">something inside the header</span>
</div>

ユーザーがスパンをクリックしたときに、divクリック イベントが発生しないようにするにはどうすればよいですか?

ベストアンサー1

使用イベント.stopPropagation()

<span onclick="event.stopPropagation(); alert('you clicked inside the header');">something inside the header</span>

IEの場合:window.event.cancelBubble = true

<span onclick="window.event.cancelBubble = true; alert('you clicked inside the header');">something inside the header</span>

おすすめ記事