简体   繁体   English

Javascript解决方法显示:IE <= 7中的table-cell

[英]Javascript work-around for display: table-cell in IE <= 7

The community has provided some wonderful tools recently to make early versions of IE do things that they were never meant to do. 社区最近提供了一些很棒的工具,可以让早期版本的IE做他们从未打算过的事情。 Like multi-column css selectors. 像多列css选择器一样。 Unfortunately I have not been able to find a java-script that I can load conditionally in IE lte 7 that converts multi-column layouts that use display: table-cell. 不幸的是我无法找到一个java脚本,我可以在IE lte 7中有条件地加载转换使用display:table-cell的多列布局。

Does anyone know of such a script? 有谁知道这样的剧本?

<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-->

There are 3 columns #side_bar and #content are columns and within #content there is #main_content and #aside_info. 共有3列#side_bar和#content是列,在#content中有#main_content和#aside_info。 This works great to create a 3 column layout on a page that has an #aside_info div but is two column other-wise. 这非常适合在具有#aside_info div的页面上创建3列布局,但在其他方面是两列。

if anyone knows a script that will convert this to tables for backward browsers I would appreciate it. 如果有人知道一个脚本将其转换为后台浏览器的表我会很感激。

Thanks, 谢谢,

Daniel 丹尼尔

It's pretty easy using jQuery: 使用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') );

Hope this helps! 希望这可以帮助!

I worked up a solution to this problem, and when I came back I saw that jimbojw had put together a much more elegant-looking solution. 我找到了解决这个问题的方法,当我回来时,我看到jimbojw已经把更优雅的解决方案组合在了一起。 I'm voting his up because its simplicity is very appealing (note to self: learn more jQuery !). 我投票支持他,因为它的简单性非常吸引人(请注意自己: 了解更多jQuery !)。 However, I'm posting my solution here anyway in case you can't use jQuery, don't know how to test for IE7, or for some other reason need a different solution: 但是,我在这里发布我的解决方案,如果你不能使用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.

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