最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jquery attr("onClick") - ie and ff - Stack Overflow

programmeradmin3浏览0评论

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

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

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

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

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

Franko

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

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

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

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

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

Franko

Share Improve this question asked Dec 2, 2009 at 16:15 frankie boylefrankie boyle 751 gold badge2 silver badges4 bronze badges 5
  • Just curious, why do you specifically need to retrieve the 'alert("boo")' in this example? Are you calling it from another place or just exploring at the moment? If you're trying to retrieve the value for another reason, there may be a different way of handling it. – JamesEggers Commented Dec 2, 2009 at 16:21
  • just exploring at the moment - was wondering why I cannot get the text / why the function is returned in ie6/7. ie8 behaves like firefox... – frankie boyle Commented Dec 2, 2009 at 16:36
  • @frankie boyle Ok. I'm curious about such now myself but wanted to make sure there wasn't a different approach if you weren't just exploring. Thanks. – JamesEggers Commented Dec 2, 2009 at 16:37
  • FWIW, if you look at the element.onclick property, as opposed to the attribute, Firefox and others will also give you the code wrapped in a function object, as this is how attribute-based event handlers are implemented. As David Dorward says below, the problem is that IE<8 (and I think IE8 in IE7 patibility mode) sees attributes and properties as the same thing, making it difficult or impossible to get the value of the onclick attribute. – NickFitz Commented Dec 2, 2009 at 17:00
  • Thank you all for your help / input! – frankie boyle Commented Dec 2, 2009 at 17:16
Add a ment  | 

5 Answers 5

Reset to default 4

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

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);

Internet Explorer <8 has a pletely broken implementation of setAttribute and getAttribute which deal with the property with the given name instead of the attribute.

I'm not aware of a work around.

Better than adding onclick attribute is behalf of IE6 / IE7 this solution:

$("a").click(function () { .. anything to do .. })

Have you tried :

alert($('a').attr("onclick"))

In XHTML onClick is not the right version.

Browser safe :

jQuery('a')[0].getAttribute('onclick');
发布评论

评论列表(0)

  1. 暂无评论