Use js native method when a triggering event triggering JQuery hover event is invalid

Demand: In the development of a climb to take chrome plug-in data from a micro-channel public number Admin pages, some page elements to show only a part of the summary information, the need to hover to display all the information (similar to the title of the pop-up display ). This requires triggering hover event that element (or mouseenter event) in chrome plugin, so that all the information is displayed and then obtain the data from the pop-window. (Due to environmental needs of customers during development to provide micro-channel public account numbers and other environmental background, this thing over a period of time, can no longer demonstrate again)

Environment: chrome browser

Problem: Common jQuery trigger event not successfully trigger events and pop-ups, symptoms manifested hover event method element itself can not trigger, but try to re-register an event (such as alert ( "test")) in the console, the longer trigger the event is successfully triggered. Toss some time, the best method to use to trigger event js native successful logic. As for jQuery why not, not yet been clarified.

solution:

  

/**
* Js native method of triggering events, some of wide applicability than jQuery method, in some cases jQuery trigger event is invalid, the application of this method is effective
*/
function triggerEvent(element,eventNameStr){
    if (document.createEvent) {
      event = document.createEvent("HTMLEvents");
      event.initEvent(eventNameStr, true, true);
      event.eventName = eventNameStr;
      element.dispatchEvent(event);
    } else {
      event = document.createEventObject();
      event.eventType = eventNameStr;
      event.eventName = eventNameStr;
      element.fireEvent("on" + event.eventType, event);
    }
}

 

Guess you like

Origin www.cnblogs.com/parker-yu/p/11103795.html