我从Typescript中得到一个错误,我不知道如何纠正它。代码工作正常时,“编译”,但我不能纠正错误。我从代码中提取了涉及错误的部分。我想我必须预测src,但不知道如何。在编辑器和上咕嘟咕嘟Typescript:属性'src'在类型'HTMLElement'上不存在
错误味精编译:
“属性 'SRC' 上型不存在“HTMLElement'.at线53栏17"
...
element:HTMLElement; /* Defining element */
'''
this.element = document.createElement('img'); /*creating a img*/
'''
这是我运行的方法来渲染元素,位置,顶部和左侧的所有工作都不会发生错误。
display() {
this.element.src = this.file; /*This is the line that gives the error*/
this.element.style.position = "absolute";
this.element.style.top = this.pointX.toString() + "px";
this.element.style.left = this.pointY.toString() + "px";
document.body.appendChild(this.element);
};
实际上,'HTMLElement.prototype'没有'src',但'HTMLImageElement.prototype'应该。我不知道TypeScript,但尝试将'element'定义为'HTMLImageElement'而不是'HTMLElement'。 – Oriol
'this.element.setAttribute(“src”,this.file);'也可以。 –
@ c-smile尽管我不认为你的答案是正确的解决方案,但我确实很欣赏以不同方式处理问题的通知 –