jQuery的ATTR在IE7 [英] jQuery attr in IE7

查看:420
本文介绍了jQuery的ATTR在IE7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下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>

和下面的JavaScript(使用jQuery):

And the following javascript (using jQuery):

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

在Firefox中此提醒图像的src属性点击,但在IE7和IE6它会提醒未定义。任何想法这是为什么,以及如何单击相应的锚标记何时返回图像的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?

编辑:
对不起球员,这里的jsfiddle例如( http://jsfiddle.net/wabqw/ )与原来的code (是的,code以上是一个简化版本)。作品在Chrome中,但没有IE浏览器(火狐无法显示图像所以没有什么可点击!)。

Sorry guys, jsFiddle example here (http://jsfiddle.net/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!).

推荐答案

我怀疑有可能是你的HTML一个问题,因为你缺少报价:

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

<img src="anImage.jpg />

<img src="anImage.jpg" />

不同的浏览器可能会以不同的方式处理这个问题。

Different browsers may handle this in different ways

对于我来说,在Chorme,它肯定不会在这个工作的jsfiddle: http://jsfiddle.net/apKdC / ,但是当我修复这个更新的提琴引号: http://jsfiddle.net/apKdC / 1 / 这一切工作正常。

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

这篇关于jQuery的ATTR在IE7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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