我正在使用jQuery(在用C#,FWIW编写的ASP.Net 2.0网站中)处理一个相当简单的div扩展和图像交换(用于显示状态指示),它的工作方式就像FF 3.5.5中的魅力一样在IE 7中失败了,我想知道你是不是很好的人可以帮我确定原因。
我正在从谷歌代码加载jQuery 1.3.2,在dom中找到一些元素,切换一些元素的可见性,然后更改displaystate图像的source属性。在Firefox中添加displaystate image的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>
没有javascript错误,并且服务器上的图像就位,IE甚至不识别src属性,因此if子句永远不会计算为true。
任何人都有任何想法,为什么我可能无法在IE中获得该图像的src?
答案 0 :(得分:1)
回顾18个月之后(我希望能看到18个月),我终于找到了问题所在。
基本上,Firefox在这里做了“错误”的事情,因为它包括父母回归prev()
电话。在此页面的html架构中,我正在寻找的<li>
包含$productContainerDiv
元素,因此prev()
在IE中没有正确返回任何内容,因为<li>
的兄弟$productContainerDiv
没有{1}}。
当我将prev("li")
更改为parent("li")
时,我得到一个能够找到由选择器("img.displayState")
定义的元素的元素。如果我当时正在处理的网站是jQuery&gt; = 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");
}
});
希望这可以帮助某个地方的人。