Home  >  Article  >  Web Front-end  >  Usage of document.createEvent in js_javascript skills

Usage of document.createEvent in js_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:20:331310browse

Comments
If the user directly views the article list, then all comments and comment boxes None of them are displayed, but if the user directly locates this blog through personal updates on other pages, such as the homepage, then all comments should be displayed. The list page and the page for viewing a single entry are the same page, which requires me to determine whether the user has located the blog. If so, trigger the click event of the A tag through JS.
At first I tried some methods, and I took it for granted that the A label had the same onclick() event as the button, but I found out that it didn’t. After searching some information on the Internet, I successfully solved the problem^_^. The solution is to write different logic for IE and FF. Part of the code is as follows:

Copy code The code is as follows:

<script> <br>var comment = document.getElementsByTagName('a')[0]; <br>if (document.all) { <br>// For IE <br>comment.click( ); <br>} else if (document.createEvent) { <br>//FOR DOM2 <br>var ev = document.createEvent('HTMLEvents'); <br>ev.initEvent('click', false, true ); <br>comment.dispatchEvent(ev); <br>} <br></script>


Syntax:
createEvent(eventType)
Parameters
Description
eventType
The event module name of the Event object you want to get.
See the "Description" section for a list of valid event types.

Return value
Returns a newly created Event object with the specified type.
Throws
If the implementation supports the required event type, this method will throw a DOMException with code NOT_SUPPORTED_ERR.
Description
This method will create a new event type, which is specified by the parameter eventType. Note that the value of this parameter is not the name of the event interface to be created, but the name of the DOM module that defines that interface.
The following table lists the legal values ​​of eventType and the event interface created by each value:
Parameters
Event interface
Initialization method
HTMLEvents
HTMLEvent
iniEvent()
MouseEvents
MouseEvent
iniMouseEvent()
UIEvents
UIEvent
iniUIEvent()

After creating the Event object using this method, you must use the method shown in the table above The initialization method initializes the object. For more information about initialization methods, see Event Object Reference.
This method is actually not defined by the Document interface, but by the DocumentEvent interface. If an implementation supports the Event module, then the Document object implements the DocumentEvent interface and supports this method.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn