<!doctype html>
<html lang="zh-CN">
    <head>
        <title>test click</title>
        <script type="text/javascript" src="test.js"></script>
    </head>
    <body>
        <center><button onclick="btnFun(this)" style="width: 80px;height: 50px;background-color: cadetblue;">hello</button></center>
    </body>
</html>

test.js

function btnFun(obj) {
    obj.style.backgroundColor = "#fdfadf";
    obj.style.width = "120px";
}

onclick 事件会在元素被点击时发生。

btnFun 中的 obj 为被点击的对象,即为调用该函数时传入的 this 指针。

 

(SAW:Game Over!)

更多推荐