jquery 在使用CSS和JavaScript的活动类期间不工作[关闭]

p5fdfcr1  于 2023-10-17  发布在  jQuery
关注(0)|答案(1)|浏览(92)

已关闭,此问题需要details or clarity。它目前不接受回答。
**想改善这个问题吗?**通过editing this post添加详细信息并澄清问题。

20小时前关闭。
Improve this question
在使用CSS和JavaScript的活动类期间,href不起作用。当添加href链接时,则不工作活动类。任何人帮助kindly href喜欢不工作在活动类使用CSS和JavaScript时添加href链接然后不工作活动类href喜欢不工作在活动类使用CSS和JavaScript时添加href链接然后不工作活动类href喜欢不工作在活动类使用CSS和JavaScript时添加href链接然后不工作活动类href喜欢不工作在活动类使用CSS和JavaScript时添加href链接然后不工作活动类HTML

<div id="sidebar" class="sidebar py-3">
 <ul class="sidebar-menu list-unstyled">

   <li class="sidebar-list-item active"><a href="dashboard.php" class="sidebar-link text- 
   muted "><i class="la la-dashboard mr-3 text-gray "></i> 
   <span>Dashboard</span></a>
   </li>

    <li class="sidebar-list-item"><a href="sales.php" class="sidebar-link text- 
   muted"><i class="la la-dashboard mr-3 text-gray "></i> 
   <span>Sales</span></a>
   </li>

   <li class="sidebar-list-item"><a href="products.php" class="sidebar-link text- 
   muted "><i class="la la-dashboard mr-3 text-gray "></i> 
   <span>Products</span></a>
   </li>

    <li class="sidebar-list-item"><a href="customers.php" class="sidebar-link text- 
   muted"><i class="la la-dashboard mr-3 text-gray "></i> 
   <span>Customers</span></a>
   </li>

   <li class="sidebar-list-item"><a href="suppliers.php" class="sidebar-link text- 
   muted "><i class="la la-dashboard mr-3 text-gray "></i> 
   <span>Suppliers</span></a>
   </li>

    <li class="sidebar-list-item"><a href="salesreport.php" class="sidebar-link text- 
   muted"><i class="la la-dashboard mr-3 text-gray "></i> 
   <span>Sales Report</span></a>
   </li>
<ul>

</div>

CSS:

@import url('https://fonts.googleapis.com/css2?family=Open+Sans&display=swap');
@import url('https://fonts.googleapis.com/css?family=Roboto');

.sidebar .active, .sidebar-link:focus {
    background: white;
    color:black !important;
    text-decoration: none;
    
  }
  .sidebar .active a{
    color: black;
    font-weight:bolder ;
  }
 .sidebar{
    position: fixed;
    top: 52px;
    box-shadow: 7px 5px 10px black;
}
.sidebar ul{
    list-style-type: none;
  margin: 0;
  padding: 0;
  width: 25%;
  background-color: #484848;
  position: fixed;
  height: 100%;
  overflow: auto;
}

 .sidebar ul li a{
    display: block;
    padding: 8px 16px;
    text-decoration: none;
    color: white;
}
.sidebar ul li a .icon{
    position: relative;
    display: block;
    min-width: 30px;
    height: 60px;
    line-height: 60px;
    text-align: center;
}
.sidebar ul li a .icon ion-icon{
    font-size: 1.5em;
}
.sidebar ul li a .item{
    position: relative;
    display: block;
    padding-left: 10px;
    height: 60px;
    line-height: 60px;
    white-space: normal;

    }
.sidebar ul li:hover{
    background: white;
    color: black;
}
.sidebar ul li a:hover{
    color: black;
    font-weight: bold;
}

JavaScript

<script type="text/javascript">
       $("ul > li").on("click", function() {
      $("ul > li").removeClass("active");
      $(this).addClass("active");
    });
</script>
vmdwslir

vmdwslir1#

问题是.sidebar .active CSS规则中的background属性覆盖了href属性。这是因为background属性比href属性具有更高的特异性。
要解决这个问题,您可以添加以下CSS规则:

.sidebar .active a {
  href: inherit;
}

这将告诉浏览器使用a元素中的href属性,即使.sidebar .active CSS规则正在设置background属性。
下面是如何使用新CSS规则的示例:

.sidebar .active, .sidebar-link:focus {
  background: white;
  color: black !important;
  text-decoration: none;
}
.sidebar .active a {
  href: inherit;
  color: black;
  font-weight: bolder;
}

您还可以删除javascript代码,当单击一个li元素时,您将使用该代码从所有其他li元素中删除active类。这是因为新的CSS规则会自动处理这个问题。
下面是一个没有javascript代码的HTML示例:

<div id="sidebar" class="sidebar py-3">
  <ul class="sidebar-menu list-unstyled">
    <li class="sidebar-list-item active"><a href="dashboard.php" class="sidebar-link text-muted "><i class="la la-dashboard mr-3 text-gray "></i> <span>Dashboard</span></a></li>
    <li class="sidebar-list-item"><a href="sales.php" class="sidebar-link text-muted"><i class="la la-dashboard mr-3 text-gray "></i> <span>Sales</span></a></li>
    <li class="sidebar-list-item"><a href="products.php" class="sidebar-link text-muted "><i class="la la-dashboard mr-3 text-gray "></i> <span>Products</span></a></li>
    <li class="sidebar-list-item"><a href="customers.php" class="sidebar-link text-muted"><i class="la la-dashboard mr-3 text-gray "></i> <span>Customers</span></a></li>
    <li class="sidebar-list-item"><a href="suppliers.php" class="sidebar-link text-muted "><i class="la la-dashboard mr-3 text-gray "></i> <span>Suppliers</span></a></li>
    <li class="sidebar-list-item"><a href="salesreport.php" class="sidebar-link text-muted"><i class="la la-dashboard mr-3 text-gray "></i> <span>Sales Report</span></a></li>
  </ul>
</div>

相关问题