let btn = document.createElement('button'); btn.textContent = 'Say Hello'; document.body.appendChild(btn); class SayHello { name: string; constructor(message: string) { this.name = message; } greet() { return 'Hello' + this.name; } } let greeting = new SayHello('World!'); btn.addEventListener('click', function () { alert(greeting.greet()); })
正如图上所显示 typescript 操作dom 和Js没有什么区别 只是提前用了es6的class语法糖