IE7中的jQuery图像src属性已损坏
我正在使用jQuery(在一个ASP.NET2.0网站中,用C#,FWIW编写)来处理一个相当简单的div扩展和图像交换(用于显示状态指示),它在FF3.5.5中工作得很好,但在IE7中失败了,我想知道你们这些优秀的人是否能够帮助我确定为什么会这样 我正在从google代码加载jQuery1.3.2,在dom中查找一些元素,在一些元素上切换可见性,然后更改displaystate图像的源属性。在Firefox中添加displaystate图像的src属性的警报会产生我所期望的结果,但在IE中它是未定义的 相关代码段如下:IE7中的jQuery图像src属性已损坏,jquery,image,attributes,Jquery,Image,Attributes,我正在使用jQuery(在一个ASP.NET2.0网站中,用C#,FWIW编写)来处理一个相当简单的div扩展和图像交换(用于显示状态指示),它在FF3.5.5中工作得很好,但在IE7中失败了,我想知道你们这些优秀的人是否能够帮助我确定为什么会这样 我正在从google代码加载jQuery1.3.2,在dom中查找一些元素,在一些元素上切换可见性,然后更改displaystate图像的源属性。在Firefox中添加displaystate图像的src属性的警报会产生我所期望的结果,但在IE中它是
<script language="javascript" type="text/javascript">
google.load("jquery","1.3.2");
</script>
<script language="javascript" type="text/javascript">
$(document).ready( function() {
$(".prodDownloadSection").click( function() {
var $catName = $(this).attr("id");
var $sectionName = $catName.substr(0,$catName.length-3);
var $productContainerDiv = $("#"+$sectionName);
$catDisplayState = $("#"+$sectionName+"displayState");
$productContainerDiv.slideToggle();
$productContainerDiv.toggleClass("selected");
$displayState = $productContainerDiv.prev("li").find("img.displayState");
alert($displayState.attr("src"));
if( $displayState.attr("src") == "img/placeholders/arrow-closed.gif") {
$displayState.attr("src", "img/placeholders/arrow-open.gif");
} else {
$displayState.attr("src", "img/placeholders/arrow-closed.gif");
}
});
});
</script>
load(“jquery”,“1.3.2”);
$(文档).ready(函数(){
$(“.prodDownloadSection”)。单击(函数(){
var$catName=$(this.attr(“id”);
var$sectionName=$catName.substr(0,$catName.length-3);
var$productContainerDiv=$(“#”+$sectionName);
$catDisplayState=$(“#”+$sectionName+“displayState”);
$productContainerDiv.slideToggle();
$productContainerDiv.toggleClass(“选定”);
$displayState=$productContainerDiv.prev(“li”).find(“img.displayState”);
警报($displayState.attr(“src”);
if($displayState.attr(“src”)==“img/placeholders/arrow closed.gif”){
$displayState.attr(“src”、“img/placeholders/arrow open.gif”);
}否则{
$displayState.attr(“src”、“img/placeholders/arrow closed.gif”);
}
});
});
没有javascript错误,并且服务器上的图像(即甚至不识别src属性)也不存在,因此if子句的计算结果永远不会为true
有人知道我为什么不能在IE中获得该图像的src吗?18个月后(我希望18个月后更明智)回顾这一点,我终于找到了问题所在 基本上,Firefox在这里做的是“错误的”事情,因为它在
prev()
调用的返回中包含了父母。在这个页面的html架构中,我要查找的
包含$productContainerDiv
元素,因此prev()
正确地在IE中不返回任何内容,因为没有
同级$productContainerDiv
当我将prev(“li”)
更改为parent(“li”)
时,我得到一个元素,它能够找到选择器(“img.displayState”)
定义的元素。如果我当时正在工作的站点是jQuery>=1.4,那么parentsUntil(“li”)将是一个更好的解决方案
事件处理程序的更正代码应如下所示:
$(".prodDownloadSection").click( function() {
var $catName = $(this).attr("id");
var $sectionName = $catName.substr(0,$catName.length-3);
var $productContainerDiv = $("#"+$sectionName);
$productContainerDiv.slideToggle();
$productContainerDiv.toggleClass("selected");
$displayState = $productContainerDiv.parents("li").find("img.displayState");
if( $displayState.attr("src") == "img/placeholders/arrow-closed.gif") {
$displayState.attr("src", "img/placeholders/arrow-open.gif");
} else {
$displayState.attr("src", "img/placeholders/arrow-closed.gif");
}
});
希望这能帮助沿途的人。$displayState.length会产生什么结果?