在使用queryselector获取一个dom元素,编译时却报错说property 'style' does not exist on type 'element'。

原因:这是typescript的类型检查导致的,需要在querySelector方法前面加个类型断言。

let frameContainObj = document.getElementById("model_view_container")
let iframeObj= <HTMLElement>frameContainObj.querySelector("#modelView");
iframeObj.style.height = document.documentElement.clientHeight* 0.65 + "px";

扩展:当使用queryselectorall时,可以采用如下方案:

let block = document.querySelectorAll(".block") as NodeListOf<HTMLElement>;

通过设置断言来告诉typescript编译器通过此代码

GitHub 加速计划 / eleme / element
13
1
下载
A Vue.js 2.0 UI Toolkit for Web
最近提交(Master分支:21 天前 )
c345bb45 1 年前
a07f3a59 * Update transition.md * Update table.md * Update transition.md * Update table.md * Update transition.md * Update table.md * Update table.md * Update transition.md * Update popover.md 1 年前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐