如何把函数声明中的事件拿出来再调用这个函数

如何改写成

function NumTotal(){
    XXXX
}XXX.onclick=NumTotal();写成这种形式

下面是代码

https://img3.mukewang.com/5bbb02b00001df6406930483.jpg

largeQ
浏览 536回答 1
1回答

繁星点点滴滴

<!DOCTYPE html><html><head>&nbsp; &nbsp; <meta charset="UTF-8">&nbsp; &nbsp; <title>Document</title>&nbsp; &nbsp; <style>&nbsp; &nbsp; &nbsp; &nbsp; div{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; width: 100px;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; height: 100px;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; background-color: red;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; </style></head><body>&nbsp; &nbsp; <div id="div1"></div>&nbsp; &nbsp; <script>&nbsp; &nbsp; &nbsp; &nbsp; var oDiv = document.getElementById('div1');&nbsp; &nbsp; &nbsp; &nbsp; oDiv.onclick = changeColor;&nbsp; &nbsp; &nbsp; &nbsp; function changeColor(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; oDiv.style.backgroundColor = 'green';&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; </script></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript