刚开始material components。我正在尝试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)
});
基本功能工作正常,我能够听到这个事件但是当我点击图标时,涟漪效果不能正常工作,图标周围的阴影永远不会。我错过了一些东西,因为这也是单选按钮吗?
答案 0 :(得分: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>