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.