首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >显示的Javascript解决方法: IE <= 7中的表格单元

显示的Javascript解决方法: IE <= 7中的表格单元
EN

Stack Overflow用户
提问于 2011-05-13 03:20:59
回答 2查看 1.4K关注 0票数 3

社区最近提供了一些很棒的工具,可以让IE的早期版本做一些他们从来不想做的事情。比如多列css选择器。不幸的是,我还没有找到一个可以在IELTE7中有条件地加载的java脚本,它可以转换使用display: table-cell的多列布局。

有人知道这样的脚本吗?

代码语言:javascript
复制
<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列布局非常有用,否则就是两列。

如果任何人知道一个脚本,可以将其转换为后向浏览器的表,我将不胜感激。

谢谢,

丹尼尔

EN

回答 2

Stack Overflow用户

发布于 2011-05-13 03:50:24

使用jQuery非常容易:

代码语言:javascript
复制
$('<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') );

希望这能有所帮助!

票数 2
EN

Stack Overflow用户

发布于 2011-05-13 04:12:04

我为这个问题设计了一个解决方案,当我回来的时候,我看到jimbojw已经组合了一个看起来更优雅的解决方案。我投票支持他,因为它的简单性非常吸引人(请注意:了解更多jQuery!)。然而,我在这里发布了我的解决方案,以防你不能使用jQuery,不知道如何测试IE7,或者因为其他原因需要一个不同的解决方案:

代码语言:javascript
复制
<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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5983305

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档