JS手動觸發事件,轉載

來源:互聯網
上載者:User

標籤:io   ar   sp   strong   on   cti   bs   html   ad   

1. createEvent(eventType)
參數:eventType 共5種類型:
    Events :包括所有的事件. 
          HTMLEvents:包括 ‘abort‘, ‘blur‘, ‘change‘, ‘error‘, ‘focus‘, ‘load‘, ‘reset‘, ‘resize‘, ‘scroll‘, ‘select‘, 
                                    ‘submit‘, ‘unload‘. 事件
          UIEevents :包括 ‘DOMActivate‘, ‘DOMFocusIn‘, ‘DOMFocusOut‘, ‘keydown‘, ‘keypress‘, ‘keyup‘.
                                  間接包含 MouseEvents. 
          MouseEvents:包括 ‘click‘, ‘mousedown‘, ‘mousemove‘, ‘mouseout‘, ‘mouseover‘, ‘mouseup‘. 
          MutationEvents:包括 ‘DOMAttrModified‘, ‘DOMNodeInserted‘, ‘DOMNodeRemoved‘, 
                                      ‘DOMCharacterDataModified‘, ‘DOMNodeInsertedIntoDocument‘, 
                                      ‘DOMNodeRemovedFromDocument‘, ‘DOMSubtreeModified‘. 
2. 在createEvent後必須初始化,為大家介紹5種對應的初始化方法
  HTMLEvents 和 通用 Events:
            initEvent( ‘type‘, bubbles, cancelable )
    UIEvents :
                      initUIEvent( ‘type‘, bubbles, cancelable, windowObject, detail )
    MouseEvents: 
                      initMouseEvent( ‘type‘, bubbles, cancelable, windowObject, detail, screenX, screenY, 
                      clientX, clientY, ctrlKey, altKey, shiftKey, metaKey, button, relatedTarget )
    MutationEvents :
                      initMutationEvent( ‘type‘, bubbles, cancelable, relatedNode, prevValue, newValue, 
                      attrName, attrChange ) 
3. 在初始化完成後就可以隨時觸發需要的事件了,為大家介紹targetObj.dispatchEvent(event)
    使targetObj對象的event事件觸發
  需要注意的是在IE 5.5+版本上請用fireEvent方法,還是瀏覽相容的考慮
4. 例子
    //例子1  立即觸發滑鼠被按下事件
    var fireOnThis = document.getElementById(‘someID‘);
        var evObj = document.createEvent(‘MouseEvents‘);
        evObj.initMouseEvent( ‘click‘, true, true, window, 1, 12, 345, 7, 220, false, false, true, false, 0, null );
        fireOnThis.dispatchEvent(evObj);

  //例子2  考慮相容性的一個滑鼠移動事件
    var fireOnThis = document.getElementById(‘someID‘);
    if( document.createEvent ) 
    {
        var evObj = document.createEvent(‘MouseEvents‘);
        evObj.initEvent( ‘mousemove‘, true, false );
        fireOnThis.dispatchEvent(evObj);
    }
  else if( document.createEventObject )
  {
      fireOnThis.fireEvent(‘onmousemove‘);//相容IE
  }

JS手動觸發事件,轉載

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.