简体   繁体   English

Google Material Design-禁用开关

[英]Google Material Design - disabling a switch

I'm unable to find any actual web based code examples that demonstrates how to go about disabling a switch for Google Material Design JavaScript. 我找不到任何实际的基于Web的代码示例,这些示例演示了如何禁用Google Material Design JavaScript的开关。

I've tried foo.setAttribute('disabled'); 我已经尝试过foo.setAttribute('disabled'); , foo.setAttribute(disabled); foo.setAttribute(disabled); , and foo.setAttribute('state', 'disabled'); ,以及foo.setAttribute('state', 'disabled'); to no avail. 无济于事。

You can disable a material design component switch with javascript simply by setting the disabled property to true. 您只需将disable属性设置为true,就可以使用javascript禁用材料设计组件开关。

const elem = document.querySelector('#switch-id');
elem.disabled = true;

So I tried benvc's solution along with a ton of different variations of it with no success - I'm sure it's just something funky with the code I'm working with. 因此,我尝试了benvc的解决方案以及其许多不同的变体,但均未获得成功-我确信它对我正在使用的代码来说很时髦。

I ended up disabling the switch by changing the element's CSS properties: 我最终通过更改元素的CSS属性来禁用开关:

elem.setAttribute("style","pointer-events: none;");

var switch = new mdc.switchControl.MDCSwitch(document.getElementByclassName('mdc-switch')); var switch = new mdc.switchControl.MDCSwitch(document.getElementByclassName('mdc-switch'));

//for enable switch.disabled = false; //对于启用switch.disabled = false;

//for disable switch.disabled = true; //对于禁用switch.disabled = true;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM