jQuery attr("onClick")-即和ff [英] jquery attr("onClick") - ie and 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屋!