使用时.style
vscode 中的 javascript 方法<script>
标签在一个html
文件中,我没有得到某些属性的任何代码完成。我正在打字b.style
,但 vscode 没有显示任何建议。我不知道为什么会这样。
<body>
<div class="body">
asdasdasdasd
</div>
<script>
let b = document.querySelector('.body');
b.style // <- no intellisense here for 'style'
</script>
<body>
问题是你有一个Element
.
Document 方法 querySelector() 返回第一个元素
- from 关于 querySelector 的 MDN 文档 https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector
and Elements
没有style
作为财产。
HTMLDivElement
确实有一个style
属性,所以你需要告诉 vscode 这就是你所拥有的。有两种方法可以做到这一点:
/** @type {HTMLDivElement} */
let b = document.querySelector('.body');
let b = /** @type {HTMLDivElement} */ (document.querySelector('.body'));
// note the cast parentheses in the second version
如果您执行其中任一操作,您将获得您想要的智能感知。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)