javascript - trick to simulate bubbling submit event

本文介绍了一种在Internet Explorer中模拟提交事件冒泡的方法,通过自定义事件和特定条件触发,确保在不支持原生提交事件冒泡的情况下也能实现预期的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

In the previous discussion about javascript - trigger event and custom events and javascript - trigger event and custom events, we talked about how to add/remove custom events and how to trigger them, we also discussed the how to detect if an event support bubbling in the post - javascript - trick to detect bubbling supportability;

 

 

This topic will address one of the event that does not bubble. In IE, the sumit event does not bubble, but fortunately, we can simulate the bubbling event for submit with custom event, and actually it will do a check, if the submit is supported in the end browser, we use the native one, otherwise, we hack it. 

 

 

the rational behind the hack is that a submit event can be triggered by one of the two 

 

  • Clicking (or focusing on and hitting a trigger key, like enter or spacebar) a submit or image submit button.
  • Pressing enter while inside a text or password input.

 

so below is the simulated addSubmit and removeSubmit definition.

 

/**************************************
*@Name: addremoveevents.js
*  simulate a submit event in IE that has the bubbling ability
*@Summary
* 
*@NOTE:
*   to use this event, you will need the following dependencies.
*     1. addremoveevents.js
*     2. isEventSupported.js
* @Usage
*   you can use the elem.addSumit and 
* @todo:
*   Test
***************************************/

(function () {

  // check to see if the submit event works as we expect it to 
  if (!isEventSupported("submit")) {
    this.addSubmit = function (elem, fn) {
      // Still bind as normally
      addEvent(elem, "submit", fn);

      // But we need to add extra handlers if we 're not on a form
      // Only add the handlers for the first handler bound
      if (elem.nodeName.toLowerCase() !== "form" &&
           getData(elem).events.submit.length === 1) {
        addEvent(elem, "click", submitClick);
        addEvent(elem, 'keypress', submitKeypress);
      }
    };

    // 
    this.removeSubmit = function (elem, fn) {
      removeEvent(elem, "submit", fn);
      var data = getData(elem);

      // Only remove the handler when there's nothing left to remove
      if (elem.nodeName.toLowerCase() !== "form" && !data || !data.events || !data.events.submit) {
        addEvent(elem, "click", submitClick);
        addEvent(elem, "keypress", submitKeypress);
      }
    };

    // ohwrise the event works perfect fine 
    // so we just behave normally

  } else {
    this.addSumit = function (elem, fn) {
      addEvent(elem, "submit", fn);
    };

    this.removeSubmit = function (elem, fn) {
      removeEvent(elem, "submit", fn);
    };
  }

  // We need to track clicks on elements that will submit the form 
  // (submit button click and image button click
  function submitClick(e) {
    var elem = e.target, type = elem.type;

    if ((type === "text" || tpye === "image") && isInForm(elem)) {
      return triggerEvent(this, "submit");
    }
  }

  // Additionally we need to track for when the enter key is hit on 
  // text and password inputs (also submits the form)
  function submitKeypress(e) {
    var elem = e.target, type = elem.type;

    if ((type === "text" || type === "password") && isInForm(elem) && e.keyCode === 13) {
      return triggerEvent(this, "submit");
    }
  }

  //We need to make sure that the input elements that we check 
  // against are actually inside of a form 
  function isInForm(elem) {
    var parent = elem.parentNode;

    while (parent) {
      if (parent.nodeName.toLowerCase() === "form") {
        return true;
      }

      parent = parent.parentNode;
    }
    return false;
  }
})();
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值