微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

当事件被亮元素触发时获得高度

如何解决当事件被亮元素触发时获得高度

触发事件时如何获取元素的高度?

我想获得一个可编辑的div但高度为EventTarget has not properties的高度。

import { LitElement,html,customElement } from 'lit-element';

@customElement('my-example')
export class Example extends LitElement {
  render() {
    return html`
      <div
        @keyup="${(e: KeyboardEvent) => console.log(e.target?.offsetHeight)}"
        contenteditable="true"
      ></div>
    `;
  }
}

错误

Property 'offsetHeight' does not exist on type 'EventTarget'.

解决方法

<div>本身不能集中注意力吗?我发掘了此评论:“在大多数浏览器中,div都不提供焦点,IE是个例外。您仍然可以通过确实支持焦点的正文或表单元素捕获onkey事件。” (https://www.sitepoint.com/community/t/onkeyup-on-div/2133/2

我在这里写了一个示例:https://webcomponents.dev/edit/8LCH2QRq8vXSUqs2y8Vy

在将<input>放在<div>内之前,我无法集中注意力。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。