样式Javascript下拉菜单
Styling Javascript dropdown
我有一个工作Javascript下拉,从Json文件拉。它工作得很好。
我正试图找出如何样式我的按钮,并使用Twitter Bootstrap 3的特点。我知道Bootstrap 3,我只是对如何访问我的javascript组件来设置它们的样式感到困惑。
这个html调用我的index.html文档中的代码
<div id='form-wrapper'></div>
这里是决定下拉菜单功能的Javascript/Json。
// The JSON this is all pulled from
var json = {
"Apple": {
"A / C": {
"weight": 6,
"price": 3.99
},
"B": {
"weight": 7,
"price": 4.99
}
},
"Orange": {
"1": {
"weight": 4,
"price": 4.49
},
"2": {
"weight": 5,
"price": 5.49
}
}
};
// Correct JSON data
for (var a in json) {
for (var attr in json[a]) {
if (/'//.test(attr)) {
var attrs = attr.split(/'s*'/'s*/);
for (var x = 0, y = attrs.length; x < y; ++ x) {
json[a][attrs[x]] = json[a][attr];
}
json[a][attr] = null;
}
}
}
var form_wrapper = document.getElementById("form-wrapper");
// Create the first dropdown
var dropdown1 = document.createElement("select");
// Populate the first dropdown
var option = document.createElement("option");
option.innerHTML = "Select a fruit";
option.value = "none";
dropdown1.appendChild(option);
// Add options from JSON
for (var attr in json) {
var option = document.createElement("option");
option.innerHTML = option.value = attr;
dropdown1.appendChild(option);
}
// Create the second dropdown
var dropdown2 = document.createElement("select");
var option = document.createElement("option");
option.innerHTML = "N/A";
dropdown2.appendChild(option);
// Add weight field
var weight = document.createElement("div");
// Add price field
var price = document.createElement("div");
// Function to automatically fill the weight/price fields
var auto_fill_fields = function() {
if (dropdown1.value != "none") {
var obj = json[dropdown1.value][dropdown2.value];
var weight_str = "Weight: " + obj.weight + " oz";
var price_str = "Price: $" + obj.price;
} else {
var weight_str = "Weight: N/A";
var price_str = "Price: N/A";
}
weight.innerHTML = weight_str;
price.innerHTML = price_str;
};
// Handle changing the first dropdown
dropdown1.addEventListener("change", function() {
// Remove the current options from the second dropdown
$(dropdown2).empty();
// Did they actually select a fruit?
if (this.value != "none") { // Yes
// Populate the second dropdown
var names = [];
for (var attr in json[this.value]) {
if (json[this.value][attr] == null) {
continue;
}
names.push(attr);
}
console.log(names);
names = names.sort();
console.log(names);
for (var x = 0, y = names.length; x < y; ++ x) {
var option = document.createElement("option");
option.innerHTML = option.value = names[x];
dropdown2.appendChild(option);
}
} else { // No
// There's nothing to choose
var option = document.createElement("option");
option.innerHTML = "N/A";
dropdown2.appendChild(option);
}
auto_fill_fields();
});
// Handle changing the second dropdown
dropdown2.addEventListener("change", auto_fill_fields);
// Fill in initial values
auto_fill_fields();
// Add submit button
var submit = document.createElement("button");
submit.innerHTML = "submit";
submit.addEventListener("click", function() {
if (dropdown1.value == "none") {
return;
}
var obj = json[dropdown1.value][dropdown2.value];
var weight_str = "Weight: " + obj.weight + " oz";
var price_str = "Price: $" + obj.price;
var request_str = dropdown1.value + " " + dropdown2.value + " - " + weight_str + ", " + price_str;
console.log(request_str);
$.ajax({
url: "SOMEPLACE",
data: {
"key": request_str
}
});
});
window.addEventListener("load", function() {
// Add the first dropdown to the page (body)
form_wrapper.appendChild(dropdown1);
// Add the second dropdown to the page (body)
form_wrapper.appendChild(dropdown2);
// And the rest
form_wrapper.appendChild(weight);
form_wrapper.appendChild(price);
form_wrapper.appendChild(submit);
});
JavaScript正在为您创建HTML。如果你安装了Chrome,右键单击页面上的下拉(Mac上的control+click),然后从菜单中选择Inspect element。(其他浏览器也可以这样做。当你按下F12时,大多数浏览器都会显示有用的开发工具。)
这将允许您探索由JavaScript生成的HTML。
UPDATE:根据评论中的后续问题,当值为N/a时,有一种方法可以附加类。这将取代原帖子中的auto_fill_fields
函数。
// Function to automatically fill the weight/price fields
var auto_fill_fields = function() {
if (dropdown1.value != "none") {
var obj = json[dropdown1.value][dropdown2.value];
var weight_str = "Weight: " + obj.weight + " oz";
var price_str = "Price: $" + obj.price;
} else {
var weight_str = "Weight: N/A";
var weight_class = "not-available";
var price_str = "Price: N/A";
var price_class = "not-available";
}
weight.innerHTML = weight_str;
if (weight_class) {
weight.className = weight_class;
}
price.innerHTML = price_str;
if (price_class) {
price.className = price_class;
}
};
我想出了一个超级简单的方法,不用改变我所做的任何事情。我只是在声明了我想要样式化的变量之后加上了3。
varName.className = "Class";
相关文章:
- JavaScript下拉菜单-点击按钮并根据所选值重定向到url
- 创建带有和不带有JavaScript的Bootstrap下拉菜单
- JavaScript下拉菜单-部件在Mac上消失
- 更改下拉菜单上的图像javascript/jquery
- Javascript-悬停时的Bootstrap下拉菜单打开子菜单的所有菜单级别
- 使用javascript onclick下拉菜单进行图像交换
- 动态下拉菜单使用JavaScript而不使用数据库
- 我的CSS/Javascript下拉菜单测试代码出了什么问题
- 日期选择器使用所有字段的下拉菜单JavaScript和JQuery W/O日历
- 下拉菜单 JavaScript
- 下拉菜单 - Javascript 与仅 CSS - 选择哪个桌面 - 移动兼容性
- 下拉菜单javascript函数
- Twitter引导下拉菜单javascript
- onmouseout的问题-简单的下拉菜单Javascript
- 可点击下拉菜单javascript
- 下拉菜单Javascript
- 下拉菜单-javascript下拉菜单,用于更改表中的所有下拉菜单
- 下拉菜单 - Javascript val() 用于选择框未正确链接
- 下拉菜单 - Javascript:关于使用 javascript 创建下拉列表的问题
- 基础站点6 -下拉菜单- javascript可以初始化