简体   繁体   中英

why does “currentTarget.onclick” have a null value even though there is an onclick handler and it works

I have set a click handler on button1, when the click handler is called it prints the id of the button and the value of "onclick" based on e.currentTarget. The callback is called corrrectly, the id is correct, BUT the "onclick" value has a value of "undefined" not something that represents the function that is called in practice (actually the value is "null" when I check in the chrome debugger). My question is 1) Why is e.currentTarget.onclick null ? 2) Where can I see if an onclick function is defined and wht it's value is ? Thanks

<body>
     <div id="top1"><button id="but1"> button 1</button></div>
</body>


function call_cb_click(text) {
    return function foo(e) {
        console.log("in callback for "+ text + "cb is " + e.currentTarget.onClick);
    }
}
function test() {
    $("#but1").on('click',call_cb_click("button 1 "));  
}
$(document).ready(test);

You didn't set an onclick function to your DOM element. It's either done with an attribute in your HTML or by using the DOM API as you did: myElement.onClick = myFunction;

Here you simply added an eventListener via jQuery. You can probably use this to get the jQuery events or use Developper tools to get the event listeners

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM