Today, when side dishes deal with the problem of cascading in the drop-down menu, they want to get the content of an event in the HTML tag, that is, the value, such as getting javascript:test(); from <select id="city" onchange="javascript:test();"></select>.
Xiaocai wanted to use the information in the event to determine the next level menu, but this seemingly simple question made Xiaocai entangled.
If you have a little understanding of JQuery's children's shoes, you may try to get it like this:
The code copy is as follows:
$(document).ready(function(){
var onchangeValue = $("#city").attr("onchange");
alert(onchangeValue);
});
Generally speaking, this can be obtained, because JQuery's universal attr method can obtain any "attribute" in the tag. Even if it is an event, the content can be directly obtained. Here onchange is an event.
But in the actual development environment, you can't get it by using this method, and all you get is undefined.
When we were struggling, we discovered another method of obtaining it using pure JavaScript.
The specific code is as follows:
The code copy is as follows:
$(document).ready(function(){
var onchangeValue = document.getElementById("city").getAttributeNode("onchange").nodeValue;
alert(onchangeValue);
});
To put it simply, the getAttributeNode() method is mainly used here. It obtains attribute nodes, ignores the difference between attributes and events, similar to processing of XML, and then uses nodeValue to obtain the node value of the attribute node.
If you use the getAttribute() method, since onchange is an event, it is obtained as a function object and cannot be processed as a string.
Hope this article can help children's shoes in need. . . . .