javascript - jQuery attr in IE7 - Stack Overflow

I have the following HTML:<a href="myPage.htm"><img src="anImage.jpg ><

I have the following HTML:

<a href="myPage.htm">
  <img src="anImage.jpg />
</a>
<a href="yourPage.htm">
  <img src="anotherImage.jpg />
</a>
<a href="everyonesPage.htm">
  <img src="stillAnotherImage.jpg />
</a>

And the following javascript (using jQuery):

$(document).ready(function(){
  $('a').live('click', function(e){
    e.preventDefault();
    alert($('img', this).attr('src'));
  });
});

In Firefox this alerts the src attribute of the image clicked, but in IE7 and IE6 it alerts 'undefined'. Any ideas why this is and how to return the src of the image when clicking the relevant anchor tag?

EDIT: Sorry guys, jsFiddle example here (/) with original code (yes, code above was a simplified version). Works in Chrome but no IE (firefox fails to display the images so there's nothing to click on!).

I have the following HTML:

<a href="myPage.htm">
  <img src="anImage.jpg />
</a>
<a href="yourPage.htm">
  <img src="anotherImage.jpg />
</a>
<a href="everyonesPage.htm">
  <img src="stillAnotherImage.jpg />
</a>

And the following javascript (using jQuery):

$(document).ready(function(){
  $('a').live('click', function(e){
    e.preventDefault();
    alert($('img', this).attr('src'));
  });
});

In Firefox this alerts the src attribute of the image clicked, but in IE7 and IE6 it alerts 'undefined'. Any ideas why this is and how to return the src of the image when clicking the relevant anchor tag?

EDIT: Sorry guys, jsFiddle example here (http://jsfiddle/wabqw/) with original code (yes, code above was a simplified version). Works in Chrome but no IE (firefox fails to display the images so there's nothing to click on!).

Share Improve this question edited Jun 1, 2011 at 17:31 thirtydot 228k50 gold badges392 silver badges354 bronze badges asked Jun 1, 2011 at 11:00 honashhonash 11 silver badge1 bronze badge 2
  • Could you make a jsFiddle test case? It seems to work fine here in IE7 using either of 1.4.4 and 1.6: http://jsfiddle/xV82c/ - if I keep the missing quotes as you had them, there's still an alert, it's just got some extra stuff. I think you've just forgotten to add the quotes in your question. – thirtydot Commented Jun 1, 2011 at 11:05
  • so... is your code right? you don't close the src? you are missing a double quote at the end of each image! You should always validate -> validator.w3 prior to ask why you get errors – balexandre Commented Jun 1, 2011 at 11:05
Add a ment  | 

4 Answers 4

Reset to default 5

I suspect there might be a problem with your HTML, as you are missing quotes:

<img src="anImage.jpg />

Should be

<img src="anImage.jpg" />

Different browsers may handle this in different ways

For me, in Chorme, it certainly doesn't work in this JSFiddle: http://jsfiddle/apKdC/, however when I fix the quotes in this updated fiddle: http://jsfiddle/apKdC/1/ it all works fine.

The real problem is that before version 9, Internet Explorer does not recognise unknown elements, such as the HTML5 elements you're using.

You can fix it by including this in your <head>:

<!--[if lt IE 9]>
<script src="//html5shim.googlecode./svn/trunk/html5.js"></script>
<![endif]-->

For some background information, read: http://paulirish./2011/the-history-of-the-html5-shiv/

Here's a page that does just that, and works in IE6/7: http://jsbin./izina3

you don't have correct html syntax see src attribute in first image missing " http://jsfiddle/HwmEK/1/ see here its working

I could be totally off-track here but shouldn't you have:

alert($('img', $(this)).attr('src'));

So $(this) not this

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745576651a4634004.html

相关推荐

  • javascript - jQuery attr in IE7 - Stack Overflow

    I have the following HTML:<a href="myPage.htm"><img src="anImage.jpg ><

    10小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信