html点击事件

当我们点击按钮执行代码,向html事件添加javascript代码 执行代码

html:

<button type="submit" id="just">Bar</button>

 js写法:

document.getElementById('#just').addEventListener('click', function() {
});//.getElementById返回拥有第一个id的指定对象
//addEventListener当该对象触发指定的事件时,指定的回调函数就会被执行。 

jquery写法:

$("#just").click(function(event) {
});

还有一种办法 是在标签中直接使用onclick例如:

<button id=just" onclick="something()">Bar</button>

 然后在单独的javascript文件中定义something();

function something(){

}

 鼠标点击时发生的点击事件

猜你喜欢

转载自1752306891.iteye.com/blog/2332743