【问题标题】:Focus on parent element when clicked is not available单击时关注父元素不可用
【发布时间】:2019-01-15 21:38:31
【问题描述】:
this.focusedElBeforeOpen = document.activeElement;

当模态关闭时,这部分代码集中在最后一个活动元素上。当单击的元素(打开模式)在下拉菜单中时出现问题,单击后,下拉菜单获取display:none。在这种情况下,我想将焦点放在父元素上。我需要纯 js 中的这个解决方案(没有 jquery)

open() { this.setSpecificContent(); this.$modal .addClass(classes.isVisible) .removeAttr('aria-hidden') .removeAttr('aria-disabled') .siblings() .attr('aria-hidden', 'true'); this.setTabIndexElementValue(-1, this.$descriptionEl); this.defer(() => this.$descriptionEl.focus(), time.animation); this.focusedElBeforeOpen = document.activeElement; }

【问题讨论】:

标签: javascript modal-dialog focus


【解决方案1】:

您可以检查该元素是否隐藏,如果是,则将父元素的引用分配给this.focusedElBeforeOpen

if (window.getComputedStyle(this.focusedElBeforeOpen).display === 'none') {
  this.focusedElBeforeOpen = this.focusedElBeforeOpen.parentElement;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-03
    • 2012-04-29
    • 2019-07-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多