物化CSS无法以编程方式打开可折叠元素

时间:2018-10-04 18:08:25

标签: javascript html css materialize

尝试在DOM中的特定可折叠元素上执行open()方法,并调用以下函数:

vm.openCollapsible = function( projectName )
{

        // This element is the span that contains the project name
        var element = getElementsByText(projectName);
        // We want to reach out its parent which has collapsible as a class and
        // it's 3 elements upwards.
        //console.log(element[0].parentElement.parentElement.parentElement);
        var collapsible_element = element[0].parentElement.parentElement.parentElement;
        var instance = M.Collapsible.getInstance(collapsible_element);
        console.log(instance);
        instance.open(0);
}

我确定选择了正确的元素,因为console.log(instance)向我返回了一个有效的对象。

s {el: ul.collapsible.collapsible-accordion, $el: g(1), options: {…}, $headers: g(1), _handleCollapsibleClickBound: ƒ, …}
$el: g [ul.collapsible.collapsible-accordion]
$headers: g [div.collapsible-header.waves-effect.project-toolbar-box]
el: ul.collapsible.collapsible-accordion
options: {accordion: true, onOpenStart: undefined, onOpenEnd: undefined, onCloseStart: undefined, onCloseEnd: undefined, …}
_handleCollapsibleClickBound: ƒ ()
_handleCollapsibleKeydownBound: ƒ ()
__proto__: s

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

使用以下命令打开所需的可折叠元素: $('。collapsible')。collapsible('open',0);

0表示索引,您应该相应地对其进行更改。