- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
在 visual studio 的默认 TypeScript HTML 应用程序中,我添加了
HTMLElement
到 window.onload 事件处理程序的第一行,认为我可以为“el”提供一个类型。
因此:
class Greeter {
element: HTMLElement;
span: HTMLElement;
timerToken: number;
constructor (element: HTMLElement) {
this.element = element;
this.element.innerText += "The time is: ";
this.span = document.createElement('span');
this.element.appendChild(this.span);
this.span.innerText = new Date().toUTCString();
}
start() {
this.timerToken = setInterval(() => this.span.innerText = new Date().toUTCString(), 500);
}
stop() {
clearTimeout(this.timerToken);
}
}
window.onload = () => {
HTMLElement el = document.getElementById('content');
var greeter = new Greeter(el);
greeter.start();
};
我得到一个错误
Compile Error. See error list for details .../app.ts (25,17): Expected ';'
有什么线索吗?我怀疑我遗漏了一些明显的东西。
最佳答案
类型在 TypeScript 中在名称之后,部分原因是类型是可选的。
所以你的台词:
HTMLElement el = document.getElementById('content');
需要更改为:
const el: HTMLElement = document.getElementById('content');
早在 2013 年,HTMLElement
类型就可以从 getElementById
的返回值中推断出来,如果您不使用 strict,情况仍然如此空值检查(但您应该使用 TypeScript 中的严格模式)。如果您执行严格的空值检查,您会发现 getElementById
的返回类型已从 HTMLElement
更改为 HTMLElement |空
。更改使类型更正确,因为您并不总能找到元素。
因此,当使用类型模式时,编译器会鼓励您使用类型断言来确保您找到了一个元素。像这样:
const el: HTMLElement | null = document.getElementById('content');
if (el) {
const definitelyAnElement: HTMLElement = el;
}
我已经包含了类型来演示运行代码时会发生什么。有趣的是,el
在 if
语句中具有更窄的类型 HTMLElement
,因为您消除了它为 null 的可能性。
您可以使用相同的结果类型做完全相同的事情,而无需任何类型注释。它们将由编译器推断,从而节省所有额外的输入:
const el = document.getElementById('content');
if (el) {
const definitelyAnElement = el;
}
关于typescript - 声明一个 HTMLElement typescript ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14742194/
我正在尝试使用 VS 2013 中的 WebBrowser 控件在网页上输入文本。我的应用程序是用 C# 编写的。网页的 Html 不使用标准 ID,而是使用称为 data-gel-id 的内容。我相
假设我有以下 HTML 代码: My first menu My second menu My title My t
在很多情况下,我已经能够以两种方式为所有 HTMLElements 注册方法。 所以我很好奇,有什么不同吗? 我应该更喜欢一种方法而不是另一种方法吗? 什么是正确的方法? 最佳答案 使用原型(prot
我创建了一个自定义事件。 var event = new Event('hello');,向节点添加监听器 div1.addEventListener('hello', /*cb1*/); div2.
[[WARNING]]:看起来像问题 TypeScript: add type property to HtmlElement但我需要向元素对象添加一个函数,而不是向节点添加属性。 我尝试将 Java
我通过 babel(es2015 预设)运行 my-widget.js 来生成 my-widget-es5.js。这会导致 Polymer 出现错误。 Class constructor Polyme
[[WARNING]]:看起来像问题 TypeScript: add type property to HtmlElement但我需要向元素对象添加一个函数,而不是向节点添加属性。 我尝试将 Java
我试图用以下设计模式写一些东西: void main() { document.registerElement('abs-test', Test); TestExtend test =
我正在尝试为 textarea html 元素引发 keydown 和 key up 事件,我可以将内部文本设置为我想要的文本,但网站似乎不承认文本在那里,除非我可以提出这些事件。我怎样才能做到这一点
使用 jQuery 可以做这样的事情:$("div")[5].animate() 在我看来,开发人员使用 prototype 扩展了 HTMLElement。 我现在的问题是:他们是怎么做到的?例如,
似乎HTMLElements默认情况下在 5 秒内等待元素。有什么方法可以更改此默认超时持续时间吗? 最佳答案 在当前版本中这是不可能的。但是我们有一个开放的拉取请求(https://github.c
https://jsfiddle.net/6vqbLm5c/29/ HTML: paragraph CSS: p { margin:0; } div { padding: 10px
如果该按钮不存在,则测试将挂起超过 5 秒。 DefaultElementLocator 中的 findElement() 方法被调用约 63 次! block 嵌套越深,等待时间越长。 是否可以在
我想检查库如何处理我传递给它的视频元素,所以我天真地这样做了: cosnt videoElement = new Proxy(document.querySelector('video'), {
我有一个 Element,但我不知道如何从中获取 HTMLElement。 例如: A link Another link 然后我像这样得到它们: var nodes: NodeListOf = do
我想从 document 获取元素高度,我正在尝试使用 const header = document.getElementsByClassName(myClass) 获取它。但是在将Element
我在将我的一个 JavaScript 行转换为 TypeScript 时遇到了一些问题。 此行用于在 ID 为 containerId 的容器上创建类型为 widgetName 的小部件。 widge
我正在尝试在 TypeScript 中实例化新的 HTMLDivElement var elem = new HTMLDivElement(); 但是浏览器抛出 Uncaught TypeError:
我想通过 javasacript 从字符串创建 HTMLElement,就像这样 element = createHTMLElement('title') element.addEventListen
如何获取插槽中 child 的 Dom 元素。试图在life hook onMounted()中获取一个对象的el vnode属性为null 我的组件: {{text}} impo
我是一名优秀的程序员,十分优秀!