gpt4 book ai didi

javascript - 动态添加菜单项不会在侧边栏导航中切换到 Sb Admin 2

转载 作者:行者123 更新时间:2023-11-28 04:28:27 26 4
gpt4 key购买 nike

我是 bootstrap 和 JQuery 的新手。我在 SB Admin 2 Bootstrap 方面遇到问题。我删除了一个示例(仪表板)页面,并动态地将一个菜单项添加到侧边栏导航中。

问题是新的菜单项不会切换/折叠。

下面是一个有效的侧边菜单 html 片段。

 <nav class="navbar navbar-default navbar-static-top" role="navigation" style="margin-bottom: 0">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#" id="navbar_brand">SB Admin v2.0</a>
</div>
<!-- /.navbar-header -->

<ul class="nav navbar-top-links navbar-right">
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">
<i class="fa fa-user fa-fw"></i> <i class="fa fa-caret-down"></i>
</a>
<ul class="dropdown-menu dropdown-user">
<li>
<a href="login.html"><i class="fa fa-sign-out fa-fw"></i> Logout</a>
</li>
</ul>
<!-- /.dropdown-user -->
</li>
<!-- /.dropdown -->
</ul>
<!-- /.navbar-top-links -->

<div class="navbar-default sidebar" role="navigation">
<div class="sidebar-nav navbar-collapse">
<ul class="nav" id="side-menu">
<li>
<a href="index.html"><i class="fa fa-dashboard fa-fw"></i> Dashboard</a>
</li>
<li>
<a href="#"><i class="fa fa-files-o fa-fw"></i> Sample Pages<span class="fa arrow"></span></a>
<ul class="nav nav-second-level">
<li>
<a href="blank.html">Blank Page</a>
</li>
<li>
<a href="login.html">Login Page</a>
</li>
</ul>
<!-- /.nav-second-level -->
</li>
</ul>
</div>
<!-- /.sidebar-collapse -->
</div>
<!-- /.navbar-static-side -->
</nav>

为了动态添加新的菜单项,我添加了一个点击事件处理程序,如下所示。

$("#navbar_brand").click(function(){
$("#side-menu").append('<li><a href="#"><i class="fa fa-files-o fa-fw"></i> TEST Pages<span class="fa arrow"></span></a><ul class="nav nav-second-level"><li><a href="blank.html">Blank Page</a></li><li><a href="login.html">Login Page</a></li></ul></li>');
});

新菜单项会出现,但不会像原始示例页面菜单一样切换。有人可以帮忙吗?

TIA

* 更新 *

请找到下面有问题的 html 文件。

<!DOCTYPE html>
<html lang="en">

<head>

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">

<title>SB Admin 2 - Bootstrap Admin Theme</title>

<!-- Bootstrap Core CSS -->
<link href="vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">

<!-- MetisMenu CSS -->
<link href="vendor/metisMenu/metisMenu.min.css" rel="stylesheet">

<!-- Custom CSS -->
<link href="dist/css/sb-admin-2.css" rel="stylesheet">

<!-- Morris Charts CSS -->
<link href="vendor/morrisjs/morris.css" rel="stylesheet">

<!-- Custom Fonts -->
<link href="vendor/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">

<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
</head>

<body>

<div id="wrapper">

<!-- Navigation -->
<nav class="navbar navbar-default navbar-static-top" role="navigation" style="margin-bottom: 0">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#" id="navbar_brand">SB Admin v2.0</a>
</div>
<!-- /.navbar-header -->

<ul class="nav navbar-top-links navbar-right">
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">
<i class="fa fa-user fa-fw"></i> <i class="fa fa-caret-down"></i>
</a>
<ul class="dropdown-menu dropdown-user">
<li>
<a href="login.html"><i class="fa fa-sign-out fa-fw"></i> Logout</a>
</li>
</ul>
<!-- /.dropdown-user -->
</li>
<!-- /.dropdown -->
</ul>
<!-- /.navbar-top-links -->

<div class="navbar-default sidebar" role="navigation">
<div class="sidebar-nav navbar-collapse">
<ul class="nav" id="side-menu">
<li>
<a href="index.html"><i class="fa fa-dashboard fa-fw"></i> Dashboard</a>
</li>
<li>
<a href="#"><i class="fa fa-files-o fa-fw"></i> Sample Pages<span class="fa arrow"></span></a>
<ul class="nav nav-second-level">
<li>
<a href="blank.html">Blank Page</a>
</li>
<li>
<a href="login.html">Login Page</a>
</li>
</ul>
<!-- /.nav-second-level -->
</li>
</ul>
</div>
<!-- /.sidebar-collapse -->
</div>
<!-- /.navbar-static-side -->
</nav>

<div id="page-wrapper">
<div class="row">
<div class="col-lg-12">
<h1 class="page-header">Dashboard</h1>
</div>
<!-- /.col-lg-12 -->
</div>
<!-- /.row -->



<div class="row">
<div class="col-lg-6">
<div class="panel panel-default">
<div class="panel-heading">
Hover Rows
</div>
<!-- /.panel-heading -->
<div class="panel-body">
<div class="table-responsive">
<table class="table table-hover">
<thead>
<tr>
<th>#</th>
<th>First Name</th>
<th>Last Name</th>
<th>Username</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Mark</td>
<td>Otto</td>
<td>@mdo</td>
</tr>
<tr>
<td>2</td>
<td>Jacob</td>
<td>Thornton</td>
<td>@fat</td>
</tr>
<tr>
<td>3</td>
<td>Larry</td>
<td>the Bird</td>
<td>@twitter</td>
</tr>
</tbody>
</table>
</div>
<!-- /.table-responsive -->
</div>
<!-- /.panel-body -->
</div>
<!-- /.panel -->
</div>
<!-- /.col-lg-6 -->

</div>
<!-- /.row -->

</div>
<!-- /#page-wrapper -->


</div>
<!-- /#wrapper -->

<!-- jQuery -->
<script src="vendor/jquery/jquery.min.js"></script>

<!-- Custom Theme JavaScript -->
<script src="vendor/core/js/common.js"></script>

<!-- Metis Menu Plugin JavaScript -->
<script src="vendor/metisMenu/metisMenu.min.js"></script>

<!-- Custom Theme JavaScript -->
<script src="dist/js/sb-admin-2.js"></script>

<!-- Bootstrap Core JavaScript -->
<script src="vendor/bootstrap/js/bootstrap.min.js"></script>


<!-- Page Custom JavaScript code -->
<script>

$( window ).on('load', function() {
console_log("page loadedd");

$(window).ready(function(){
console_log("page ready");

// setup page
$.pageInit();
});
});

$.pageInit = function() {

}

$("#navbar_brand").click(function(){
console_log("test");

$("#side-menu").append('<li><a href="#"><i class="fa fa-files-o fa-fw"></i> TEST Pages<span class="fa arrow"></span></a><ul class="nav nav-second-level"><li><a href="blank.html">Blank Page</a></li><li><a href="login.html">Login Page</a></li></ul></li>');


});

</script>
</body>
</html>

最佳答案

很难确切地说 #side-menu 是什么功能是无法查看您正在使用的 JS 脚本内部,但您需要注意 JavaScript 的工作顺序。这就是您的浏览器正在执行的操作:

  1. 阅读 HTML 文档。
  2. 加载并执行所有 .js 文件。在这里,您的插件正在寻找 #side-menu并将功能应用于其现有的 2 个 <li>子元素。
  3. 点击 #navbar_brand 后,它添加了第三个 <li> child 到 #side-menu ,但这个函数从未添加任何功能,因为那是在步骤 2 中发生的。

您有 2 个选择:

  1. 查看该插件的功能,看看您是否可以在添加第三个 <li>再次启动它 child 。
  2. 拥有全部 3 个 <li>内置于 HTML 文档中,但具有 display: none在第三个的 CSS 中。一旦您点击#navbar_brand您可以通过将其样式更改为 display: block 来显示它或者任何你想要的。

关于javascript - 动态添加菜单项不会在侧边栏导航中切换到 Sb Admin 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44832196/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com