JavaScript gets code for event values such as OnClick, onchange, etc.

Source: Internet
Author: User

Here is the main use of GetAttributeNode () This method, it gets the attribute node, ignoring the difference between the attributes and events, such as the following, interested friends can refer to the next ha hope to be helpful to everyone

Today, when dealing with drop-down menu cascading issues, you want to get the content of an event in the HTML tag, which is the value, such as from <select id= "City" onchange= "Javascript:test ();" ></select> get Javascript:test ();. Side dishes want to use the information in the event to determine the next level of the menu, but this seemingly simple problem, but let the side of the knot.
A little bit of jquery's children's shoes may try to get:
$ (document). Ready (function() {    var onchangevalue = $ ("#city"). attr ("onchange");    alert (onchangevalue);});

In general, this can indeed be obtained, because the jquery almighty attr method can get any "attribute" in the tag, even if it is an event, it can get the content directly, here onchange is the event.
But the side dishes in the actual development environment, with this method how also can not obtain, get is undefined.
At the time of the tangle, another method was found to achieve access using pure JavaScript.
The specific code is as follows:

$ (document). Ready (function() {    var onchangevalue = document.getElementById ("City"). GetAttributeNode ("onchange"). NodeValue;    alert (onchangevalue);});

To put it simply, the main thing here is to use the GetAttributeNode () method, which gets the attribute node, ignores the difference between the attribute and the event, resembles the processing of the XML, and then uses NodeValue to get the node value of the attribute node.
If you use the GetAttribute () method, because OnChange is an event, you get a function object that cannot be treated as a string.

JavaScript gets code for event values such as OnClick, onchange, etc.

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.