0

刚从材料组件开始。我正在尝试mdc-icon-toggle,HTML 代码

<i class="mdc-icon-toggle material-icons" role="button" aria-pressed="false" aria-label="Add to favorites" tabindex="0"         
    data-toggle-on='{"label": "Remove from favorites", "content": "favorite"}'
    data-toggle-off='{"label": "Add to favorites", "content": "favorite_border"}' 
    data-mdc-auto-init="MDCIconToggle">
      favorite_border
 </i>

JS代码

const iconEl = document.querySelector('.mdc-icon-toggle');

iconEl.addEventListener('MDCIconToggle:change', ({detail}) => {
  alert(detail.isOn)
});

基本功能工作正常,我可以收听事件,但是当我单击图标时,涟漪效果无法正常工作,图标周围有阴影,从不消失了。我是否也缺少单选按钮?

4

1 回答 1

0

我现在正在学习MDC。

使用此表单更好地工作,将 javascript 代码放在 BODY 标记的末尾:

<i class="mdc-icon-toggle material-icons" role="button" aria-pressed="false" aria-label="Add to favorites" tabindex="0"         
    data-toggle-on='{"label": "Remove from favorites", "content": "favorite"}'
    data-toggle-off='{"label": "Add to favorites", "content": "favorite_border"}'>
      favorite_border
 </i>


<script type="text/javascript">
(function() {
      /* MDC Ripple for Toggle button */
      var nodes = document.querySelectorAll('.mdc-icon-toggle');
      for (var i = 0, node; node = nodes[i]; i++) {
        mdc.iconToggle.MDCIconToggle.attachTo(node);
      }
})();
</script>
于 2018-07-30T16:23:38.327 回答