Jquery替换已存在于element上的event的方法
作者:bea
看下面code: 代码如下: <head> <meta content="text/html; charset=utf-8" http-equiv="Content-Type" /> <title>TestPage</title> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" type="text/javasc
看下面code:
代码如下:
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>TestPage</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#btn1").unbind('click').removeAttr('onclick').click(function() {
alert('The method has invoked by Jquery.');
});
});
function foo()
{
alert('The method has invoked.');
}
</script>
</head>
<body>
<input type="button" value="ClickMe" id="btn1" onclick="foo()" />
</body>
</html>
用的就是unbind,然后再removeAttr,最后再加上你的方法。来看下它们API document:
unbind([type], [fn])
概述
bind()的反向操作,从每一个匹配的元素中删除绑定的事件。
如果没有参数,则删除所有绑定的事件。
你可以将你用bind()注册的自定义事件取消绑定。
如果提供了事件类型作为参数,则只删除该类型的绑定事件。
如果把在绑定时传递的处理函数作为第二个参数,则只有这个特定的事件处理函数会被删除。
参数
type (可选)String
事件类型
fn (可选)Function
要从每个匹配元素的事件中反绑定的事件处理函数
removeAttr(name)
概述
从每一个匹配的元素中删除一个属性
参数
nameString
要删除的属性名
简单的就是:
代码如下:
$("#someelement").unbind('eventname').removeAttr('event').click(function() {
//your new method.
});
希望对您有帮助。
有用 | 无用
代码如下:
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>TestPage</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#btn1").unbind('click').removeAttr('onclick').click(function() {
alert('The method has invoked by Jquery.');
});
});
function foo()
{
alert('The method has invoked.');
}
</script>
</head>
<body>
<input type="button" value="ClickMe" id="btn1" onclick="foo()" />
</body>
</html>
用的就是unbind,然后再removeAttr,最后再加上你的方法。来看下它们API document:
unbind([type], [fn])
概述
bind()的反向操作,从每一个匹配的元素中删除绑定的事件。
如果没有参数,则删除所有绑定的事件。
你可以将你用bind()注册的自定义事件取消绑定。
如果提供了事件类型作为参数,则只删除该类型的绑定事件。
如果把在绑定时传递的处理函数作为第二个参数,则只有这个特定的事件处理函数会被删除。
参数
type (可选)String
事件类型
fn (可选)Function
要从每个匹配元素的事件中反绑定的事件处理函数
removeAttr(name)
概述
从每一个匹配的元素中删除一个属性
参数
nameString
要删除的属性名
简单的就是:
代码如下:
$("#someelement").unbind('eventname').removeAttr('event').click(function() {
//your new method.
});
希望对您有帮助。
有用 | 无用
猜你喜欢
您可能感兴趣的文章:
- JS在IE和FireFox之间常用函数的区别小结
- javascript offsetX与layerX区别
- jQuery 点击图片跳转上一张或下一张功能的实现代码
- javascript 图片上一张下一张链接效果代码
- JQuery Ajax 跨域访问的解决方案
- javascript 不用reverse实现字符串反转的代码
- ExtJS 学习专题(一) 如何应用ExtJS(附实例)
- jquery 简单的进度条实现代码
- 精通JavaScript 纠正 cleanWhitespace函数
- javascript模拟滚动条实现代码
- Javascript 入门基础学习
- javascript showModalDialog,open取得父窗口的方法
- ExtJs3.0中Store添加 baseParams 的Bug
- js Date自定义函数 延迟脚本执行
- javascript select options 排序(保持option 对象完整性)
- js 蒙版进度条(结合图片)
- javascript 事件查询综合 推荐收藏
- JQuery的html(data)方法与<script>脚本块的解决方法
- JQuery+div+css 无限级联树实现代码