css 如何删除所选选项卡的下边框

yzuktlbb  于 2023-06-25  发布在  其他
关注(0)|答案(4)|浏览(123)

如何修改我当前的HTML/CSS编码,以便一旦选择了选项卡,底部边框将被删除?
以下是它目前的样子:

以下是预期的结果:

下面是代码:

<!DOCTYPE html>
<html>
  <head>
    <title>JavaScript tabs example</title>

    <style type="text/css">
        ul#tabs { list-style-type: none; margin: 30px 0 0 0; padding: 0 0 0.3em 0; }
        ul#tabs li { display: inline; }
        ul#tabs li a { color: #42454a; background-color: #dedbde; border: 1px solid #c9c3ba; border-bottom: none; padding: 0.3em; text-decoration: none; }
        ul#tabs li a:hover { background-color: #f1f0ee; }
        ul#tabs li a.selected { color: #000; background-color: #fff; font-weight: bold; }

        div.tabContent { border: 1px solid #c9c3ba; padding: 0.5em; background-color: #fff; }
        div.tabContent.hide { display: none; }

      #file_content {
        width: 100%;
        border: 1px solid red;
      }
      #file_content td {
        padding: 5px;

      }

    </style>

    <script type="text/javascript">

    var tabLinks = new Array();
    var contentDivs = new Array();

    function init() {

      // Grab the tab links and content divs from the page
      var tabListItems = document.getElementById('tabs').childNodes;
      for ( var i = 0; i < tabListItems.length; i++ ) {
        if ( tabListItems[i].nodeName == "LI" ) {
          var tabLink = getFirstChildWithTagName( tabListItems[i], 'A' );
          var id = getHash( tabLink.getAttribute('href') );
          tabLinks[id] = tabLink;
          contentDivs[id] = document.getElementById( id );
        }
      }

      // Assign onclick events to the tab links, and
      // highlight the first tab
      var i = 0;

      for ( var id in tabLinks ) {
        tabLinks[id].onclick = showTab;
        tabLinks[id].onfocus = function() { this.blur() };
        if ( i == 0 ) tabLinks[id].className = 'selected';
        i++;
      }

      // Hide all content divs except the first
      var i = 0;

      for ( var id in contentDivs ) {
        if ( i != 0 ) contentDivs[id].className = 'tabContent hide';
        i++;
      }
    }

    function showTab() {
      var selectedId = getHash( this.getAttribute('href') );

      // Highlight the selected tab, and dim all others.
      // Also show the selected content div, and hide all others.
      for ( var id in contentDivs ) {
        if ( id == selectedId ) {
          tabLinks[id].className = 'selected';
          contentDivs[id].className = 'tabContent';
        } else {
          tabLinks[id].className = '';
          contentDivs[id].className = 'tabContent hide';
        }
      }

      // Stop the browser following the link
      return false;
    }

    function getFirstChildWithTagName( element, tagName ) {
      for ( var i = 0; i < element.childNodes.length; i++ ) {
        if ( element.childNodes[i].nodeName == tagName ) return element.childNodes[i];
      }
    }

    function getHash( url ) {
      var hashPos = url.lastIndexOf ( '#' );
      return url.substring( hashPos + 1 );
    }

    window.onload = function() {
        init()
    }

    </script>
  </head>
  <body>





        <table id="file_content" cellspacing="0" cellpadding="0">
            <tr><td>File Content</td></tr>
            <tr>
                <td>
                    <ul id="tabs">
                        <li><a href="#about">About JavaScript tabs</a></li>
                        <li><a href="#advantages">Advantages of tabs</a></li>
                        <li><a href="#usage">Using tabs</a></li>
                    </ul>

                    <div class="tabContent" id="about">
                      <h2>About JavaScript tabs</h2>
                      <div>
                        <p>JavaScript tabs partition your Web page content into tabbed sections. Only one section at a time is visible.</p>
                        <p>The code is written in such a way that the page degrades gracefully in browsers that don't support JavaScript or CSS.</p>
                      </div>
                    </div>

                    <div class="tabContent" id="advantages">
                      <h2>Advantages of tabs</h2>
                      <div>
                        <p>JavaScript tabs are great if your Web page contains a large amount of content.</p>
                        <p>They're also good for things like multi-step Web forms.</p>
                      </div>
                    </div>

                    <div class="tabContent" id="usage">
                      <h2>Using tabs</h2>
                      <div>
                        <p>Click a tab to view the tab's content. Using tabs couldn't be easier!</p>
                      </div>
                    </div>

                </td>
            </tr>
            <tr>
                <td></td>
            </tr>
        </table>

  </body>
</html>

d5vmydt9

d5vmydt91#

将所有选项卡设置为position:relativetop:1px。这将使它们全部向下移动1px。给予它们都一个z-index:-1,这将使它们出现在静态内容下。将活动选项卡设置为具有z-index:1或高于0的值。这确保它位于顶部并覆盖顶部灰色边框。

ul#tabs li a {
    position:relative;
    top:-1;
    z-index:-1;
}

ul#tabs li a.selected {
    z-index:1;
}

Working Fiddle

kx1ctssn

kx1ctssn2#

您可以向 selected 选项卡添加border-bottom以覆盖灰色线,如下所示:

ul#tabs li a.selected {
    color: #000;
    background-color: #fff;
    font-weight: bold;
    border-bottom: 2px solid #fff;
}

*WORKING DEMO

nukf8bse

nukf8bse3#

处理这一问题的有效方法是将边界定义为内部边界。这可以通过使用插入box-shadow来实现。这样就不需要定义负边距或相对偏移来掩盖边界,因为它是内部边界。

header {
    box-shadow: inset 0 -1px grey;
    background: linear-gradient(whitesmoke, lightgrey);
    padding: 0.25em 1em 0 1em;
}
header h1 {
    font-size: 1.25em;
}
header > menu.tabs {
    display: flex;
    gap: 1em;
    padding: 0;
    margin: 0;
    list-style-type: none;
}
header > menu.tabs > li > a {
    border: 1px solid transparent;
    border-radius: 0.25em 0.25em 0 0;
    border-bottom: none;
    display: block;
    padding: 0.25em 1em;
    text-decoration: none;
    color: black;
}

header > menu.tabs > li:hover > a {
    background: whitesmoke;
}
header > menu.tabs > li.active > a {
    border-color: grey;
    background: white;
    font-weight: bold;
}
<header>
  <h1>Heading</h1>
  <menu class="tabs">
    <li class="active"><a href="">Overview</a></li>
    <li><a href="">Documents</a></li>
    <li><a href="">Settings</a></li>
  </menu>
</header>
t2a7ltrp

t2a7ltrp4#

在你的文件里第11行

ul#tabs li a.selected { color: #000; background-color: #fff; font-weight: bold; border-bottom: #fff 2px solid;}

相关问题