This article describes how to use JavaScript to obtain the values of onclick, onchange, and other events. For more information about how to solve the cascading problem of the drop-down menu, to obtain the content of an event in the HTML Tag, that is, the value, for example, from To obtain javascript: test ();.
The dish wants to determine the next menu through the information in the event, but this seemingly simple problem makes the dish tangle.
If you have a little understanding of JQuery, you may try to get it like this:
The Code is as follows:
$ (Document). ready (function (){
Var onchangeValue = $ ("# city"). attr ("onchange ");
Alert (onchangeValue );
});
In general, this can be obtained, because JQuery's omnipotent attr method can be used to obtain any "attribute" in the tag. Even if it is an event, the content can be directly obtained, here onchange is an event.
However, in the actual development environment, this method cannot be obtained, and all the results are undefined.
During the tangle, we found another method to implement acquisition using pure JavaScript.
The Code is as follows:
The Code is as follows:
$ (Document). ready (function (){
Var onchangeValue = document. getElementById ("city"). getAttributeNode ("onchange"). nodeValue;
Alert (onchangeValue );
});
To put it simply, the getAttributeNode () method is used here. It obtains attribute nodes and ignores the differences between attributes and events, similar to XML processing, then, use nodeValue to obtain the node value of the attribute node.
If the getAttribute () method is used, because onchange is an event, a function object is obtained and cannot be processed as a string.
Hope this article can help the children's shoes in need .....