Document. createEvent usage in js

Source: Internet
Author: User

<A class = "comment-mod" onclick = "alert ('ss')" href = "#"> comment </a>
If you directly view the document list, all comments and comments are not displayed. However, if you dynamically locate this log on other pages, such as the homepage, then all comments should be displayed. The list page is the same as the page for viewing a single entry, which requires me to determine whether the user has located the log. If yes, trigger the Click Event of the tag through JS.
At first, I tried some methods and thought that the tag and the button had an onclick () event. I found no. After searching some information on the Internet, successfully solved this problem ^_^. The solution is to write different logic for IE and FF. Some code is as follows:
Copy codeThe Code is as follows:
<Script>
Var comment = document. getElementsByTagName ('A') [0];
If (document. all ){
// For IE
Comment. click ();
} Else if (document. createEvent ){
// FOR DOM2
Var ev = document. createEvent ('htmlevents ');
Ev. initEvent ('click', false, true );
Comment. dispatchEvent (ev );
}
</Script>


Syntax:
CreateEvent (eventType)
Parameters
Description
EventType
The name of the Event module of the Event object to be obtained.
For a list of valid event types, see the description section.

Return Value
Returns the newly created Event object with the specified type.
Throw
If the implementation supports the required event type, this method throws the DOMException code NOT_SUPPORTED_ERR.
Description
This method creates a new event type, which is specified by the eventType parameter. Note: The value of this parameter is not the name of the event interface to be created, but the name of the DOM module of that interface.
The following table lists the valid values of eventType and the event interfaces created for each value:
Parameters
Event Interface
Initialization Method
HTMLEvents
HTMLEvent
IniEvent ()
MouseEvents
MouseEvent
IniMouseEvent ()
UIEvents
UIEvent
IniUIEvent ()

After you create an Event object using this method, you must use the initialization method shown in the table to initialize the object. For more information about the initialization method, see the Event object reference.
This method is not defined by the Document interface, but by the deletevent interface. If an implementation supports the Event module, the Document Object implements the deleentevent interface and supports this method.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.