jquery在IE中找不到图像

jquery在IE中找不到图像,jquery,internet-explorer,class,attributes,find,Jquery,Internet Explorer,Class,Attributes,Find,回答 我有一段jquery代码可以在FF中工作,但不能在IE中工作。更让我困惑的是,一段几乎相同的代码可以在两者中工作 我有一个类为imghead和imghead2的div。根据第一个img标记的src,我想附加一段代码 此代码在两种浏览器中都适用: $(".imghead2 img[src=/img/image.gif]").each(function() { $(".imghead2").prepend('<a href="#"><img src="/img

回答

我有一段jquery代码可以在FF中工作,但不能在IE中工作。更让我困惑的是,一段几乎相同的代码可以在两者中工作

我有一个类为imghead和imghead2的
div
。根据第一个
img
标记的src,我想附加一段代码

此代码在两种浏览器中都适用:

$(".imghead2 img[src=/img/image.gif]").each(function() {
        $(".imghead2").prepend('<a href="#"><img src="/img/image" border="0" id="anniversary" alt="" /></a>');          
 });

=
之前添加了一个*并且现在似乎运行良好:

$(".imghead img[src*='/img/mh_image.jpg']").each(function() {
        $(".imghead").prepend('<a href="#"><img src="/img/image.png" border="0" id="anniversary" alt="" /></a>');
         });
$(“.imghead img[src*='/img/mh_image.jpg'])。每个(函数(){
$(“.imghead”)。前缀(“”);
});

由于“2”,我猜一个有效,另一个无效。你在里面有很多。这很奇怪,我会检查W3C的有效id文档。我也这么认为,但带有“2”的文档实际上在两种浏览器中都能工作。此外,我建议将您的代码更改为
$(“.imghead img[src='/img/mh_image.jpg'])
,这样就没有怪癖的余地了……我无法测试这一点,因为我讨厌在IE中调试JS,但这可能与SRC标记的解析方式有关。有些浏览器会将HREF重写为完全限定的路径(包括域名),我打赌这同样适用于图像上的SRC。试着使用contains选择器
~=
,看看它是否有效。从技术上讲,jquery属性选择器中的值周围必须加引号:我个人不太喜欢使用引号,因为这似乎是“可选的必须”选项之一,但因为您使用的是潜在危险的字符(斜杠),我还会尝试添加引号。
<div id="header">
    <div class="imghead2" style="float:right"><img src="/img/image.gif" alt="" width="314" height="11" border="0" /></div>
    <div class="imghead" style="float:left"><a href="/"><img src="/img/mh_image.jpg" alt="" width="260" height="60" border="0" /></a><noscript><p class="noScriptHead">This page uses Javascript. Your browser either doesn't support Javascript or you have it turned off.<br />To see this page as it is meant to appear please use a Javascript enabled browser.</p></noscript></div>
</div>
$(".imghead img[src*='/img/mh_image.jpg']").each(function() {
        $(".imghead").prepend('<a href="#"><img src="/img/image.png" border="0" id="anniversary" alt="" /></a>');
         });