提问者:小点点

通过Javascript创建手风琴


我使用的是Javascript,没有jQuery。

到目前为止,我已经完成了手风琴标题面板:

var accordion = document.createElement('div');
            accordion.className = 'panel-group';
            accordion.id = 'accordion';
            document.getElementById('navgroupsframe').appendChild(accordion);

            var panel = document.createElement('div');
            panel.className = 'panel';
            panel.style.marginLeft = '-15px';
            accordion.appendChild(panel);

            var panelheading = document.createElement('div');
            panelheading.className = 'panel-heading';
            panel.appendChild(panelheading);

            var paneltitle = document.createElement('div');
            paneltitle.className = 'vgroupholder panel-title'

但是,我想要到这里:

<div class="panel-group" id="accordion">
                            <div class="panel" style="margin-left: -15px;">
                            <div class="panel-heading">
                                <div class="vgroupholder panel-title" data-parent="#accordion" data-toggle="collapse" href="#title1" onclick="removevBgs();">

我该怎么做? (只使用Javascript添加data-parent等,而不使用jQuery)。

谢了。


共1个答案

匿名用户

您可以考虑先创建父元素,然后用旧的innerHTML注入其余元素,因为您没有使用JQuery:)

var accordion = document.createElement('div');
accordion.className = 'panel-group';
accordion.id = 'accordion';

accordion.innerHTML = `<div class="panel" style="margin-left: -15px;">
                            <div class="panel-heading">
                                <div class="vgroupholder panel-title" data-parent="#accordion" data-toggle="collapse" href="#title1" onclick="removevBgs();"></div>
                             </div>
                       </div>`;

document.getElementById('navgroupsframe').appendChild(accordion);