[英]Javascript work-around for display: table-cell in IE <= 7
社区最近提供了一些很棒的工具,可以让早期版本的IE做他们从未打算过的事情。 像多列css选择器一样。 不幸的是我无法找到一个java脚本,我可以在IE lte 7中有条件地加载转换使用display:table-cell的多列布局。
有谁知道这样的剧本?
<div id="sidebar">
<ul id="main_nav">
<li><a href="about_us.php">ABOUT US</a></li>
</ul>
</div> <!--end of sidebar-->
<div id="content">
<div id="main_content">
<h1>Header</h1>
<p>Page Content</p>
</div> <!--end of main_content-->
<div class="aside_info">
<h2>Side Info</h2>
</div>
</div> <!--end of content-->
共有3列#side_bar和#content是列,在#content中有#main_content和#aside_info。 这非常适合在具有#aside_info div的页面上创建3列布局,但在其他方面是两列。
如果有人知道一个脚本将其转换为后台浏览器的表我会很感激。
谢谢,
丹尼尔
使用jQuery非常简单:
$('<table><tr><td></td><td></td><td></td></tr></table>')
.find('td')
.eq(0)
.append( $('#sidebar') )
.end()
.eq(1)
.append( $('#main_content') )
.end()
.eq(2)
.append( $('.aside_info') )
.end()
.end()
.appendTo( $('#content') );
希望这可以帮助!
我找到了解决这个问题的方法,当我回来时,我看到jimbojw已经把更优雅的解决方案组合在了一起。 我投票支持他,因为它的简单性非常吸引人(请注意自己: 了解更多jQuery !)。 但是,我在这里发布我的解决方案,如果你不能使用jQuery,不知道如何测试IE7,或者由于某些其他原因需要一个不同的解决方案:
<div id="sidebar">
<ul id="main_nav">
<li><a href="about_us.php">ABOUT US</a></li>
</ul>
</div> <!--end of sidebar-->
<div id="content">
<div id="main_content">
<h1>Header</h1>
<p>Page Content</p>
</div> <!--end of main_content-->
<div class="aside_info">
<h2>Side Info</h2>
</div>
</div> <!--end of content-->
<script type="text/javascript">
// Test for IE version
var ieversion = 0;
if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ // IE test adapted from http://www.javascriptkit.com/javatutors/navigator.shtml
ieversion = new Number(RegExp.$1)
}
if(ieversion > 0 && ieversion < 8)
{
// Find the existing DIV elements
var sidebarDiv = document.getElementById("sidebar");
var mainContentDiv = document.getElementById("main_content");
var contentDiv = document.getElementById("content");
// Create the structure of the table that will replace them
var tableElement = document.createElement("table");
var tbodyElement = document.createElement("tbody");
var trElement = document.createElement("tr");
var sidebarTd = document.createElement("td");
sidebarTd.id = "sidebar";
var mainContentTd = document.createElement("td");
mainContentTd.id = "main_content";
var asideInfoTd = document.createElement("td");
// Clone the DIVs' child nodes and add the clones to the appropriate TDs. Then add the TDs to the TR.
for(var i=0;i<sidebarDiv.childNodes.length;i++)
{
sidebarTd.appendChild(sidebarDiv.childNodes[i].cloneNode(true));
}
trElement.appendChild(sidebarTd);
for(var i=0;i<mainContentDiv.childNodes.length;i++)
{
mainContentTd.appendChild(mainContentDiv.childNodes[i].cloneNode(true));
}
trElement.appendChild(mainContentTd);
mainContentDiv.parentNode.removeChild(mainContentDiv);
var contentChildDivs = contentDiv.getElementsByTagName("div");
for(var i=0;i<contentChildDivs.length;i++)
{
if(contentChildDivs[i].className.indexOf("aside_info") > -1)
{
asideInfoTd.appendChild(contentChildDivs[i].cloneNode(true));
}
}
// We only add the aside info if there were some (sometimes there aren't)
if(asideInfoTd.childNodes.length > 0)
{
trElement.appendChild(asideInfoTd);
}
// Finish putting the table together
tbodyElement.appendChild(trElement);
tableElement.appendChild(tbodyElement);
// Remove the existing content div and then replace the sidebar with the new table that contains all 2 or 3 columns.
contentDiv.parentNode.removeChild(contentDiv);
sidebarDiv.parentNode.replaceChild(tableElement, sidebarDiv);
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.