简体   繁体   中英

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. Like multi-column css selectors. 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.

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. This works great to create a 3 column layout on a page that has an #aside_info div but is two column other-wise.

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:

$('<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. I'm voting his up because its simplicity is very appealing (note to self: learn more 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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