简体   繁体   English

使“被选中”class成为启动的一部分

[英]make "is-selected" class a part of startup

I am trying to make a toggle button be ON by default when the webpage loads.我试图在网页加载时默认打开切换按钮。 I am using Microsoft's Fabric UI.我正在使用 Microsoft 的 Fabric UI。

When I try to add is-selected to <label for="demo-toggle-3" class="ms-Toggle-field" tabindex="0"> it will remove the is-selected when the page starts.当我尝试将is-selected添加到<label for="demo-toggle-3" class="ms-Toggle-field" tabindex="0">时,它会在页面启动时删除is-selected However, I can add it later to the tag by using the inspection tools in Chrome.但是,我可以稍后使用 Chrome 中的检查工具将其添加到标签中。

Here is the code I used:这是我使用的代码:

HTML HTML

<div class="ms-Toggle">
  <span class="ms-Toggle-description">Let apps use my location</span> 
  <input type="checkbox" id="demo-toggle-3" class="ms-Toggle-input" />
  <label for="demo-toggle-3" class="ms-Toggle-field is-selected" tabindex="0">
    <span class="ms-Label ms-Label--off">Off</span> 
    <span class="ms-Label ms-Label--on">On</span> 
  </label>
</div>

JavaScript JavaScript

<script type="text/javascript">
  var ToggleElements = document.querySelectorAll(".ms-Toggle");
  for (var i = 0; i < ToggleElements.length; i++) {
    new fabric['Toggle'](ToggleElements[i]);
  }
</script>

However when the page loads, it comes out like this:但是当页面加载时,它是这样的:

<div class="ms-Toggle">
  <span class="ms-Toggle-description">Let apps use my location</span> 
  <input type="checkbox" id="demo-toggle-3" class="ms-Toggle-input" />
  <label for="demo-toggle-3" class="ms-Toggle-field" tabindex="0">
    <span class="ms-Label ms-Label--off">Off</span> 
    <span class="ms-Label ms-Label--on">On</span> 
  </label>
</div>

Thanks to @HereticMonkey I was able to solve the issue by adding the is-selected class to the toggle element after its been created.感谢@HereticMonkey,我能够通过在创建切换元素后将is-selected class 添加到切换元素来解决该问题。 Since I have multiple toggles, I used a forloop like so:因为我有多个切换,所以我使用了一个 forloop,如下所示:

for (var i = 0; i < ToggleElements.length; i++) {ToggleElements[i].querySelector('.ms-Toggle-field').classList.add('is-selected')}

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

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