简体   繁体   English

仅在 IE7 中显示/隐藏 div 问题

[英]Show/hide div problems in IE7 only

I wondered whether someone might be able to help?我想知道是否有人可以提供帮助? I've tried and tried to find a solution myself, but nothing seems to work.我已经尝试并尝试自己找到解决方案,但似乎没有任何效果。

I have a horizontal list and when the user clicks on one of these links, a hidden div appears just below the list, filling the width of the overall container (950px).我有一个水平列表,当用户单击其中一个链接时,列表下方会出现一个隐藏的 div,填充整个容器的宽度(950 像素)。

This works absolutely perfectly on Firefox, Safari and IE8 but doesn't seem to work on IE7 (and possibly less, I haven't been able to check).这在 Firefox、Safari 和 IE8 上绝对完美,但似乎不适用于 IE7(可能更少,我无法检查)。

In IE7, the div causes the list to break, plonking the final list item on an extra line and (as a result, I presume?) pushing the div further down the page, so it's not flush with the bottom of the list.在 IE7 中, div 会导致列表中断,将最终列表项放在额外的一行上,并且(结果,我猜?)将 div 推到页面下方,因此它不会与列表底部齐平。 In fact, it appears just beneath the div with ID "highlightsbar".事实上,它出现在 ID 为“highlightsbar”的 div 下方。

Here is the relevant code - I'd be eternally grateful for any suggestions anyone might have!这是相关代码 - 我将永远感谢任何人可能提出的任何建议!

You can see this problem 'in action' at http://www.totalbackpacker.co.uk .您可以在http://www.totalbackpacker.co.uk看到这个问题“正在发生”。 (Interestingly, if I do a quick test with only the relevant bits of code at http://www.martinjefferies.co.uk/test.html , the problem isn't there. I'm not sure if that helps or not?!) (有趣的是,如果我只使用http://www.martinjefferies.co.uk/test.html的相关代码位进行快速测试,问题就不存在了。我不确定这是否有帮助?!)

Thanks,谢谢,

Martin马丁

HTML: HTML:

<div id="outer">

<div id="wrapper">

<div id="header">
</div>

<div id="navbar">

<ul>
<li class="left"><a href="#" title="Home"><img src="<?php bloginfo('template_url'); ?>/images/navbar/home.png" alt="Home" /></a></li>
<li><a href="#" title="Explore by country" onClick="toggle('submenu')"><img src="<?php bloginfo('template_url'); ?>/images/navbar/explorebycountry.png" alt="Explore by country" /></a></li>
<li><a href="#" title="Search"><img src="<?php bloginfo('template_url'); ?>/images/navbar/search.png" alt="Search" /></a></li>
<li><a href="#" title="Contact"><img src="<?php bloginfo('template_url'); ?>/images/navbar/contact.png" alt="Contact" /></a></li>
<li class="right"><a href="#" title="About"><img src="<?php bloginfo('template_url'); ?>/images/navbar/about.png" alt="About" /></a></li>

<div id="submenu" style="display: none; z-index:500;">
<div id="submenu-inner">

<?php
$categories = get_categories('child_of=7');
$count = 1; ?>
<div class="left">
Left hand links go here
</div>
<div class="right">
Right hand links go here
</div>
<div class="clearer"></div>
<br /><a href="#" title="Close menu" onClick="toggle('submenu')">Close menu</a>
</div>
</div>

</ul>
<div class="clearer"></div>

<div id="highlightsbar">
<span class="title">Promotion:</span> Promotion info goes here.
</div><!--highlightsbar-->
</div><!--navbar-->

<div id="content">

</div>

</div>

</div>

CSS: CSS:

#outer {
margin:0 auto;
background:#E2E2E2;
width:100%;
}

#wrapper {
text-align:left;
width:950px;
margin-left:auto;
margin-right:auto;
background:#FFFFFF;
padding:0 0 50px 0;
}

#header {
background:#be023a;
height:100px;
width:950px;
margin:0;
padding:0;
}

#navbar {
background:#cc0000 url('http://www.totalbackpacker.co.uk/wp-content/themes/totalbackpacker/images/navbar.jpg') repeat-x;
height:70px;
width:950px;
}

#navbar ul {
float:left;
list-style:none;
margin:7px 0 0 10px;
padding:0;
height:40px;
}

#navbar li {
float:left;
}

#navbar li a {
display:block;
padding:3px 10px;
margin:0;
border-right:1px solid #ffffff;
font-family:Helvetica,Arial,sans-serif;
font-size:15px;
line-height:15px;
color:#ffffff;
text-decoration:none;
text-transform:uppercase;
font-weight:normal;
}

#navbar li a:hover {
background:#cc0000;
}

#navbar img {
border:0;
}

#highlightsbar {
padding:0;
margin:3px 0 0 20px;
font-family:Helvetica,Arial,sans-serif;
font-size:12px;
line-height:12px;
color:#666666;
text-decoration:none;
}

#highlightsbar .title {
text-transform:uppercase;
float:left;
font-weight:bold;
}

#highlightsbar .textwidget {
float:left;
padding:0;
margin:0 0 0 5px;
}

.clearer {
clear:both;
}

#submenu {
background:url('../images/submenushadow.png') left bottom repeat-x;
margin:30px 0 0 -10px;
padding:0 0 50px 0;
z-index:5000;
position:relative;
width:950px;
display:block;
}

#submenu-inner {
background:#ffffff;
border-left:5px solid #be023a;
border-bottom:5px solid #be023a;
border-right:5px solid #be023a;
padding:20px;
}

#submenu-inner .right {
float:left;
width:140px;
padding:0;
margin:0;
}

#submenu-inner .left {
float:left;
width:140px;
padding:0;
margin:0;
}

JavaScript: JavaScript:

<script type="text/javascript" language="javascript">
function toggle(id)
{
    el = document.getElementById(id);
    var display = el.style.display ? '' : 'none';
    el.style.display = display;
}
window.onload=function()
{document.getElementById('submenu').style.display='none';}

</script>

If you pull it out and it works then it must be something in the surrounding code.如果您将其拉出并且可以正常工作,那么它一定是周围代码中的某些内容。

The most common issue with non javascript IE compatibility is IE will treat extra open tags and close tags differently than other browsers.非 javascript IE 兼容性的最常见问题是 IE 将与其他浏览器不同地处理额外的打开标签和关闭标签。

I suggest you look very closely at the html, or selectively add back HTML till you can reproduce.我建议您仔细查看 html,或选择性地添加回 HTML 直到您可以重现。

I suggest you look into using conditional-includes for IE7 (forget about IE6 if at all possible).我建议您考虑为 IE7 使用条件包含(如果可能,请忘记 IE6)。 Here's a jsFiddle of the code I got working in my copy of IE7: http://jsfiddle.net/qjx4g/2/这是我在 IE7 副本中工作的代码的 jsFiddle: http://jsfiddle.net/qjx4g/2/

What happened is that you shouldn't have your submenu code within your #navbar <ul> (ie, close your #navbar ( </ul> ) and then put in your submenu's code) and whenever I have problems with a container with position: relative that contains content that is float ed, I change the container to use position: absolute and work from there (only in a conditional-include for IE7).发生的事情是,您不应该在#navbar <ul>中包含子菜单代码(即关闭您的#navbar ( </ul> ),然后放入子菜单的代码),并且每当我遇到带有position: relative的容器时position: relative包含float的内容,我将容器更改为使用position: absolute并从那里工作(仅在 IE7 的条件包含中)。

1 - If you give your ul a width of 100%, it solves the problem of the last list item "About" pushing itself down to a second line. 1 - 如果你给你的 ul 一个 100% 的宽度,它解决了最后一个列表项“关于”将自己推到第二行的问题。

2 - Close your ul. 2 - 关闭你的 ul。 In IE9 the div#submenu is a child of ul, while in IE7 it is the child of the last li在 IE9 中,div#submenu 是 ul 的子项,而在 IE7 中,它是最后一个 li 的子项

3 - div#navbar{ position: relative;} - div#submenu{position: absolute; 3 - div#navbar{ position:相对;} - div#submenu{位置:绝对; top: 15px;顶部:15px; right: 1px;} - Works on IE7右:1px;} - 适用于 IE7

pic图片

Here's the solution I came up with.这是我想出的解决方案。

Firstly, I created an extra div for the submenu - called #submenu-outer :首先,我为子菜单创建了一个额外的 div - 称为#submenu-outer

#submenu-outer {
position:absolute;
width:100%;
left:0;
display:block;
text-align:center;
margin:35px 0 0 0;
z-index:5000;
}

Then I made #submenu appear in the centre of the 100% outer div:然后我让#submenu出现在 100% 外部 div 的中心:

#submenu {
background:url('totalbackpacker.co.uk/wp-content/themes/totalbackpacker/images/…;) left bottom repeat-x;
margin-left:auto;
margin-right:auto;
padding:0 0 50px 0;
width:950px; 
text-align:left;
}

Et voila: Thanks to everyone who helped along the way! Et voila:感谢一路上提供帮助的所有人! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM