javascript - Property 'querySelector' does not exist on type 'Node'.
迷茫
迷茫 2017-04-10 15:37:18
0
1
592

这个是我的代码

<!DOCTYPE html>
<html>
<head lang="zh-CN">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1"/>
    <title></title>
</head>
<body>
    <p id="container">
        <p id="box"></p>
    </p>
    <script src="test.js"></script>
</body>
</html>

这个是test.ts文件

var box = document.querySelector('#box');
console.log(box.parentNode.querySelector('#box'));

额,这行代码有问题:

Error:(2, 28) TS2339: Property 'querySelector' does not exist on type 'Node'.

但是我发现MDN的文档有如下说明

parentNode is the parent of the current node. The parent of an element is an Element node, a Document node, or a DocumentFragment node.

然后我又进行了如下测试

var ul = document.querySelector('ul')
undefined
ul.parentNode.toString()
[object HTMLpElement]"

好吧,parentNode返回的类型不是Node,拿这个报错又是怎么回事
typescript的版本是1.4

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

Antworte allen(1)
刘奇

HTMLpElement继承自Node 也能用querySelector吧

Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!