- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 jQuery 的新手,使用它已经 3 天了,在提问之前我已经查看了这里的所有问题。我想使用 json 文件中的数据用下拉菜单填充导航栏,我能够完美地解析数据,但是用于 Bootstrap 下拉菜单的用法,我正在动态添加来自 jQuery 的类,但它们根本不起作用,请帮忙。
这是我的 index.html
<html lang="en" xmlns="http://www.w3.org/1999/html">
<head>
<title>JavaScipt AJAX</title>
<link rel="stylesheet" type="text/css" href="bootstrap.min.css"/>
<link rel="stylesheet" type="text/css" href="main.css"/>
</head>
<body>
<div class="navbar secondary-navbar" id="department-nav">
<div class="navbar-inner" id="menu">
</div>
</div>
<script src="jquery.min.js"></script>
<script src="script_menu.js"></script>
<script src="bootstrap.min.js"></script>
</body>
</html>
sript_menu.js
$( document ).ready(function() {
$.getJSON( "data.json", function(data) {
console.log( data );
var menu_item = '<ul class="nav nav-departments nav-featured">';
var output = '<ul class="nav-list">';
$.each(data, function(key, val) {
if(val.warehouse_id=="1"){
if(val.name=="Produce" || val.name=="Bakery" || val.name=="Dairy & Eggs" || val.name=="Beverages" ) {
menu_item += '<li class="dropdown">';
menu_item += '<a class="navbar-link dropdown-toggle">' + val.name + '</a>';
output+= '<li>';
output+= '<h2>' + val.name + '</h2>';
menu_item += '<ul class="dropdown-menu">';
for(var i=0; i<val.aisles.length; i++) {
output+= '<p>' + val.aisles[i].name + '</p>';
menu_item += '<li>' + val.aisles[i].name + '</li>';
}
menu_item += '</ul>';
menu_item += '</li>';
output+= '</li>';
}
}
});
output+= '</ul>';
menu_item+= '</ul>';
$('#menu').html(menu_item);
$('#update').html(output);
});
});
这是我的 data.json
[
{
"display_name": "Beer, Wine & Spirits",
"id": 148,
"name": "Beer, Wine & Spirits",
"rank_offset": 991,
"sort_order": 9,
"visible": true,
"warehouse_id": 2,
"aisles": [{
"department_id": 148,
"id": 979,
"name": "Spirits",
"rank_offset": 1,
"visible": true
}, {
"department_id": 148,
"id": 976,
"name": "Beer",
"rank_offset": 2,
"visible": true
}]
}, {
"display_name": "Prepared Food",
"id": 59,
"name": "Prepared Food",
"rank_offset": 990,
"sort_order": 3,
"visible": true,
"warehouse_id": 2,
"aisles": [{
"department_id": 59,
"id": 776,
"name": "Tofu & Soy",
"rank_offset": 45,
"visible": true
}, {
"department_id": 59,
"id": 465,
"name": "Salads, Sushi & Wraps",
"rank_offset": 10,
"visible": true
}, {
"department_id": 59,
"id": 466,
"name": "Sides & Soups",
"rank_offset": 40,
"visible": true
}, {
"department_id": 59,
"id": 468,
"name": "Whole Meals",
"rank_offset": 20,
"visible": true
}, {
"department_id": 59,
"id": 764,
"name": "Fresh Pasta",
"rank_offset": 35,
"visible": true
}, {
"department_id": 59,
"id": 770,
"name": "Desserts",
"rank_offset": 50,
"visible": true
}, {
"department_id": 59,
"id": 771,
"name": "Fresh Pizza",
"rank_offset": 30,
"visible": true
}]
}, {
"display_name": "Grocery",
"id": 57,
"name": "Grocery",
"rank_offset": 997,
"sort_order": 5,
"visible": true,
"warehouse_id": 2,
"aisles": [{
"department_id": 57,
"id": 443,
"name": "Spices",
"rank_offset": 100,
"visible": true
}, {
"department_id": 57,
"id": 444,
"name": "Baking",
"rank_offset": 100,
"visible": true
}, {
"department_id": 57,
"id": 445,
"name": "Condiments",
"rank_offset": 100,
"visible": true
}, {
"department_id": 57,
"id": 446,
"name": "Peanut Butter & Jelly",
"rank_offset": 100,
"visible": true
}, {
"department_id": 57,
"id": 447,
"name": "Oil, Vinegar & Tuna",
"rank_offset": 100,
"visible": true
}, {
"department_id": 57,
"id": 448,
"name": "Oil & Olives",
"rank_offset": 100,
"visible": true
}, {
"department_id": 57,
"id": 449,
"name": "Canned & Jarred Vegetables",
"rank_offset": 100,
"visible": true
}, {
"department_id": 57,
"id": 450,
"name": "Tomato Sauce",
"rank_offset": 100,
"visible": true
}, {
"department_id": 57,
"id": 451,
"name": "Pasta",
"rank_offset": 100,
"visible": true
}, {
"department_id": 57,
"id": 452,
"name": "Grains, Rice & Dried Beans",
"rank_offset": 100,
"visible": true
}, {
"department_id": 57,
"id": 453,
"name": "Soup",
"rank_offset": 100,
"visible": true
}]
}, {
"display_name": "Personal & Home Care",
"id": 56,
"name": "Personal & Home Care",
"rank_offset": 994,
"sort_order": 6,
"visible": true,
"warehouse_id": 2,
"aisles": [{
"department_id": 56,
"id": 435,
"name": "Vitamins & Supplements",
"rank_offset": 30,
"visible": true
}, {
"department_id": 56,
"id": 436,
"name": "Paper Products",
"rank_offset": 20,
"visible": true
}, {
"department_id": 56,
"id": 438,
"name": "Pets!",
"rank_offset": 40,
"visible": true
}, {
"department_id": 56,
"id": 437,
"name": "Cleaning Products",
"rank_offset": 10,
"visible": true
}, {
"department_id": 56,
"id": 778,
"name": "Personal Care",
"rank_offset": 28,
"visible": true
}, {
"department_id": 56,
"id": 779,
"name": "Soap",
"rank_offset": 25,
"visible": true
}]
}
]
感谢您的耐心等待..
最佳答案
您无法在加载文档后仅使用类名来初始化 Bootstrap 组件。当您将类名添加到组件时,Bootstrap 会在页面加载时初始化它们。如果您在页面加载后添加组件,您需要使用 Bootstrap 文档中列出的“通过 JavaScript”初始化方法自行初始化它,即对于下拉列表,您需要触发 $('.dropdown-toggle ').dropdown()
( Bootstrap docs )
关于javascript - 从 jQuery AJAX 动态创建 Bootstrap 下拉列表不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22317904/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!