3

I'm starting to learn JavaScript and I would know how to call a function from an event. The only way I know is with anonymous functions (see my code) and I don't know how to call it in a simpler way. Thanks in advance!

    function example (){
        document.getElementById("myDiv").onclick = function (){
            example2 ();
        }
    }
4

2 回答 2

4

You can just assign it by name:

function example (){
    document.getElementById("myDiv").onclick = example2;
}
于 2013-03-21T11:16:26.620 回答
1

Try

document.getElementById('myDiv').onclick=example2;
于 2013-03-21T11:17:19.187 回答