Home >
Article > Web Front-end > JavaScript code to obtain onclick, onchange and other event values_jquery
JavaScript code to obtain onclick, onchange and other event values_jquery
WBOYOriginal
2016-05-16 17:28:09952browse
When Xiaocai dealt with the cascading problem of drop-down menus today, he wanted to get the content of an event in the HTML tag, that is, the value, for example, from
$(document).ready(function(){ var onchangeValue = $("#city").attr("onchange"); alert(onchangeValue); });
Under normal circumstances, this can indeed be obtained, because JQuery's universal attr method can obtain any "attribute" in the tag, even if it is an event, you can also directly obtain the content, here onchange is event. But in the actual development environment, Xiaocai cannot be obtained using this method, and all obtained are undefined. While struggling, I found another way to obtain it 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 getAttributeNode() method is mainly used here. It obtains the attribute node, ignoring the difference between attributes and events, similar to the 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, what you get is a function object and cannot be treated as a string. I hope this article can help children’s shoes in need.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn