/ 设置 10 秒超时 // 每日统计清 0 runtime_set('todaycomments', 0); runtime_set('todayarticles', 0); runtime_set('todayusers', 0); if ($forumlist) { $fidarr = array(); foreach ($forumlist as $fid => $forum) { $fidarr[] = $forum['fid']; } forum_update($fidarr, array('todayposts' => 0, 'todaythreads' => 0)); } // 清理临时附件 attach_gc(); // 当天24点 $today = strtotime(date('Ymd')) + 86400; runtime_set('cron_2_last_date', $today, TRUE); // 往前推8个小时,尽量保证在前一天 升级过来和采集的数据会很卡 // table_day_cron($time - 8 * 3600); cache_delete('cron_lock_2'); } } } ?>javascript - jquery onclick penetrating to parent element - Stack Overflow|Concepts Of Algorithm

javascript - jquery onclick penetrating to parent element - Stack Overflow

admin2025-04-03  2

How would I stop onclick for an inner element from also clicking through to its parent element.

<div id="1" onclick="alert('1')">
<div id="2" onclick="alert('2')"></div>
</div>

If the second div (id="2") is visibly on top of the first, and it is clicked, You will get both alerts 1 & 2.

How can I avoid this so I only get the alert from what I'm (visibly) clicking on, Without pletely disabling any onclick function the outer div may possess.

Example: /

How would I stop onclick for an inner element from also clicking through to its parent element.

<div id="1" onclick="alert('1')">
<div id="2" onclick="alert('2')"></div>
</div>

If the second div (id="2") is visibly on top of the first, and it is clicked, You will get both alerts 1 & 2.

How can I avoid this so I only get the alert from what I'm (visibly) clicking on, Without pletely disabling any onclick function the outer div may possess.

Example: http://jsfiddle/DPCx8/

Share Improve this question asked May 11, 2012 at 6:56 d-_-bd-_-b 23.2k43 gold badges171 silver badges285 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 7
​var outer = document.getElementById('outer');
outer.onclick = function(event)
    {
        alert('outer');
        event.stopPropagation();
    }​​
    var inner = document.getElementById('inner');
inner.onclick = function(event)
    {
        alert('inner');
        event.stopPropagation();
    }

http://jsfiddle/DYykA/1/

You need to stop the propagation at the child, so the event doesn't bubble up to the parent.

$("#inner").on("click", function(e){
  e.stopPropagation();
});

use event.stopPropogation(); to prevent bubbling of event

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1743628464a213844.html

最新回复(0)