当jQuery .attr('onclick')函数返回事件对象时? [英] When jQuery .attr('onclick') function return a event object?

查看:157
本文介绍了当jQuery .attr('onclick')函数返回事件对象时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是调试FireBug中的jQuery和有关

I am just debugging jQuery in FireBug and wonderig about the return value of

$('.a-selector').attr('onclick');

原来是onclick(event),但是我之前已经阅读了一些代码,而作者只是这样使用它:

It turns out to be an onclick(event), but I have read some code before and the author just uses it like this:

$('.a-selector').attr('onclick').replace(..., ...);

表示可以将其视为字符串对象.但是当我这样使用时,它报告一个错误.

which means it can be treated as a String Object. But it reports an error when I use like this.

我的jQuery版本是1.5.2.因此,我想知道jQuery何时更改API,以及更改HTML中定义的onclick事件的最佳方法是什么.

My jQuery version is 1.5.2. So I wonder when the jQuery changes the API and what is the best way to change the onclick event defined in the HTML.

推荐答案

当jQuery .attr('onclick')函数返回事件对象时?

When jQuery .attr('onclick') function return a event object?

在jQuery中< 1.6.这是因为.attr()在1.6之前的版本中,在合适的位置检索属性和属性之间进行了混合,较新的版本删除了该层巫术,现在具有用于检索属性的适当方法( .prop ).

In jQuery < 1.6. That's because prior to 1.6, .attr() did a mix between retrieving properties and attributes where it saw fit, newer versions removed that layer of witchery and now have proper methods for retrieving attributes (.attr) and properties (.prop).

这是小提琴,展示了上述内容.

Here's a fiddle demonstrating the above.

ps.顺便说一句,它不返回事件对象,而是一个充当事件处理程序的函数对象. =]

ps. BTW, it doesn't return an event object, but rather a function object that serves as event handler. =]

此外,还有2个注意事项:可行时,应始终将jquery升级到最新版本(当前为1.8.3),它具有更多功能,更好的性能和许多错误修复.

Also, 2 side notes: You should always upgrade your jquery to the latest version when viable (currently 1.8.3), it comes with more features, better performance and lots of bug fixes.

使用jQuery时,您不应该真正使用onclick,这违反了Web 2.0的结构(html)和行为(js)分离标准-jQuery本身提供了跨浏览器处理程序,并附加了方法 .on() (适用于jQuery 1.7+)和 .delegate / .live (对于较早的版本).

And you shouldn't really be using onclicks when you have jQuery, that goes against the Web 2.0 standards of separation of structure (html) and behavior (js) - jQuery itself provides cross-browser handler attaching with the methods .on() (for jQuery 1.7+), and .bind/.delegate/.live for older versions.

这篇关于当jQuery .attr('onclick')函数返回事件对象时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆