jQuery attr("onClick")-即和ff [英] jquery attr("onClick") - ie and ff

查看:126
本文介绍了jQuery attr("onClick")-即和ff的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用jquery从A标签的onClick属性中获取javascript.

Using jquery, I'd like to get the javascript from an A tag's onClick attribute.

<a href='#' onClick='alert("boo");' />

在Firefox中:alert($('a').attr("onClick"))显示:alert("boo")

In Firefox: alert($('a').attr("onClick")) shows: alert("boo")

在IE 6/7中:alert($('a').attr("onClick"))显示:function anonymous(){alert("boo");return false;}

In IE 6/7: alert($('a').attr("onClick")) shows: function anonymous(){alert("boo");return false;}

如何使用jQuery在IE 6/7中仅检索JavaScript,而不检索包装函数? (或纯JavaScript)?

How can I retrieve just the javascript, and not the wrapped function, in IE 6/7 using jquery? (or plain javascript)?

Franko

推荐答案

如何在IE 6/7中仅检索javascript,而不检索包装函数

How can I retrieve just the javascript, and not the wrapped function, in IE 6/7

您通常根本根本不希望内联事件处理程序使用字符串值(实际上,您通常应该完全避免使用内联事件处理程序属性,而倾向于绑定到脚本中的函数),尤其是在使用jQuery时,以这种方式为标准).但是,如果必须的话,解决方法是使用DOM方法getAttributeNode.

You generally don't want to rely on string values for inline event handlers at all (in fact you should generally avoid using inline event handler attributes altogether in favour of binding to functions from script — especially if you're using jQuery, where this approach is the norm). But if you have to, the workaround is the DOM method getAttributeNode.

var link= $('a')[0]; // or whatever
alert(link.getAttributeNode('onclick').value);

这篇关于jQuery attr("onClick")-即和ff的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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