MDL:将开关状态设置为“开".通过JS [英] MDL: Set Switch state to "on" via JS

查看:54
本文介绍了MDL:将开关状态设置为“开".通过JS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以通过 let iSwitch = document.getElementById('interestedSwitch');

我看了一下github,发现方法 MaterialSwitch.on(),但是 iSwitch.MaterialSwitch.on(); 未定义. iSwitch.checked = true; 也不起作用.如果这可能是问题的根源,我正在使用AngularJS.

I looked at github and found the method MaterialSwitch.on(), but iSwitch.MaterialSwitch.on(); is undefined. iSwitch.checked = true; does not help either. I am using AngularJS, if this could be a source of the problem.

推荐答案

假设 interestedSwitch 是类为 mdl-switch__input 的输入元素,则需要访问父元素.这应该是带有 mdl-js-switch (带有 MaterialSwitch 组件)的标签.然后,您可以尝试以下操作:

Assuming interestedSwitch is the input element with class mdl-switch__input you need to access the parent element. This should be the label with mdl-js-switch (with MaterialSwitch component). Then you can try something like:

if(parent) {
        if(parent.MaterialSwitch && parent.MaterialSwitch.checkToggleState) {
            /* update MDL state if it was changes form javascript */
            parent.MaterialSwitch.checkToggleState();
        }
    }

这篇关于MDL:将开关状态设置为“开".通过JS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆