jQuery替换已存在于元素element上的事件event

<html>
<head>
<script type="text/javascript" src="/jquery/jquery.js"></script>
<script type="text/javascript">

function test()
{
alert('这是测试方法');
}

function newFun(msg)
{
alert('这是新方法,参数是:'+msg);
}

//实现代码

$("#btnTest").unbind('click').removeAttr('event').click(function() { 

//新方法,如果要替换的新方法已经存在,直接写方法名加参数即可,例如 newFun('测试'); 如果新方法不存在,直接写方法体即可,例如 alert('这是测试方法');

});

//代码分析,1.先通过jQuery的unbind('事件名称,如click')方法解绑click事件,然后removeAttr('onclick'),就能把onclick属性给去掉了 ,最后通过jQuery的bind()或者直接click()来重新绑定我们的click事件

</script>
</head>
<body>
<input id='btnTest' type="button" value="按钮" onclick="test()"/>
</body>
</html>

原文地址:https://www.cnblogs.com/programsky/p/5324004.html