OnClick事件被触发两次

OnClick事件被触发两次

本文介绍了OnClick事件被触发两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循以下遗留代码,仅在firefox中被触发两次,请让我知道需要做什么

I have following legacy code which gets fired twice only in firefox, please let me know what needs to be done

<div onclick="$get('imgLookUpIcon').click();" style="width: 25px; height: 56px;">
    <span style="visibility: none;"></span>
    <span></span>
    <img src="./images/lookup.gif" id="imgLookUpIcon" style="vertical-align: middle;" onclick="javascript:ABCDEF(event,this);"/>
</div>

请告诉我我想到的两个解决方案是什么原因

Kindly do let me know what will be the cause two solutions what i think of are

  1. 删除img中的点击
  2. 停止传播.

问候塞尔瓦·库玛(Selva Kumar J)

RegardsSelva Kumar J

推荐答案

您必须使用event.stopPropagation()停止将事件传播到父树.

You have to use event.stopPropagation() that stop the event propagation to the parent tree.

尝试 FIDDLE

由于您尚未提供$ get实现,因此我已将代码编码为如下所示的模拟功能

As you have not provide the $get implementation, i have coded as a mock function like below

  function $get(idselector) {
    alert('$get');
    return $('#' + idselector);
  }

  function ABCDEF(event, object) {
    alert('ABCDEF');
    event.stopPropagation(); // try commenting this will reproduce your issue
  }

希望它对您有用

这篇关于OnClick事件被触发两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 16:58