-2
<select name="pList" id="pList" style="display: none">
<option value="">--ProductA--</option>
<option value="productA">ProdA</option>
<option value="productA">ProdA</option>
<option value="productA">ProdA</option>
<option value="">--ProductB--</option>
<option value="productB">ProdB</option>
<option value="productB">ProdB</option>
<option value="">--ProducstC--</option>
<option value="productC">ProdC</option>
<option value="productC">ProdC</option>
<option value="productC">ProdC</option>
</select>
我試圖將上述選擇值轉換成手風琴,我能夠成功地爲標題創建新的按鈕元素,但是當我嘗試追加每個元素時爲孩子選擇值,按鈕(應該工作作爲手風琴)內,無法正常工作,它只是增加值的標題,請參閱: https://jsfiddle.net/bernlt/txgnk89w/5/Append Div on button
var element = document.getElementById('pList');
var children = element.children;
var filtered = [];
var filterItens = [];
// Create Headings for the Button e.g: --ProductA--/--ProductB--/--ProductC--
for (var i = 0; i < children.length; i++) {
if (children[i].textContent.startsWith('--')) {
filtered.push(children[i].textContent); //Heading labels
}else{
// separate array listing all products
filterItens.push(children[i].textContent);
}
}
var prods= filtered.toString().split(',');
var itens = filterItens.toString().split(',');
for (var i in prods) {
var newElement = document.createElement('button');
newElement.className = "accordion";
newElement.innerHTML = prods[i];
document.body.appendChild(newElement);
}
for (var i = 0; i < children.length; i++) { // get arraylist values
if (children[i].value=="productA"){
var foo = document.getElementById("--ProductA--");
var newElement = document.createElement('div');
newElement.id = children[i].value;
newElement.className = "productA";
newElement.innerHTML = children[i].text;
foo.appendChild(newElement);
}
if (children[i].value=="productB"){
var foo = document.getElementById("--ProductB--");
var newElement = document.createElement('div');
newElement.id = children[i].value;
newElement.className = "productB";
newElement.innerHTML = children[i].text;
foo.appendChild(newElement);
}
if (children[i].value=="productC"){
var foo = document.getElementById("--ProducstC--");
var newElement = document.createElement('div');
newElement.id = children[i].value;
newElement.className = "productC";
newElement.innerHTML = children[i].text;
foo.appendChild(newElement);
}
}
我接受任何其他建議,如這是我的學習路徑aprt,基本上我試圖用select創建手風琴,所以任何幫助都會很好。