[英]Typescript: Property 'src' does not exist on type 'HTMLElement'
I get a error from Typescript and I am not sure how to correct it.我从 Typescript 收到一个错误,我不知道如何纠正它。 The code works fine when "compiled" but I can't correct the error.
“编译”时代码工作正常,但我无法纠正错误。 I have extracted the parts that involve the error from my code.
我已经从我的代码中提取了涉及错误的部分。 I guess I have to predifine the src but not sure how.
我想我必须预先定义 src 但不确定如何。
Error msg in Editor and on Gulp compile:编辑器中的错误消息和 Gulp 编译:
"Property 'src' does not exist on type 'HTMLElement'.at line 53 col 17" “'HTMLElement'类型不存在属性'src'。在第53行第17行”
...
element:HTMLElement; /* Defining element */
'''
this.element = document.createElement('img'); /*creating a img*/
'''
This is the method I run to render the element, position, top and left all works with out giving a error.这是我用来渲染元素 position 的方法,顶部和左侧都可以正常工作而不会出现错误。
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);
};
使用铸造:
(<HTMLImageElement>document.querySelector(".company_logo")).src
Because src
is not a property of the HTMLElement
type, but of HTMLImageElement
. 因为
src
不是HTMLElement
类型的属性,而是HTMLImageElement
。
If you are certain you'll get an img element, you might want to declare your variable with the correct subtype: 如果您确定您将获得img元素,则可能需要使用正确的子类型声明您的变量:
element: HTMLImageElement; /* Defining element */
// ...
this.element = document.createElement('img'); /*creating a img*/
Also, you might want to have a look at what document.createElement
returns. 此外,您可能想要查看
document.createElement
返回的内容。 It's the very same type if you specify "img"
as its argument. 如果您指定
"img"
作为其参数,则它是完全相同的类型。
go with: go 与:
var myImg = document.getElementById('myId') as HTMLImageElement;
myImg.src = "imgSrc";
document.getElementById('img').setAttribute( 'src', 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==' );
通过set属性方法设置source。
We can solve it with this two ways..我们可以用这两种方法解决它..
CASE - 1情况1
Use <HTMLImageElement>
before document.getElementById('myId')
在
document.getElementById('myId')
之前使用<HTMLImageElement>
var myImg = <HTMLInputElement>document.getElementById('myId');
myImg.src = 'https://picsum.photos/id/237/200/300';
CASE - 2案例 - 2
Use setAttribute
to set src
to the image
as below使用
setAttribute
将src
设置为image
,如下所示
var myImg = document.getElementById('myId');
myImg.setAttribute('src', 'https://picsum.photos/id/237/200/300');
You can cast the target element from the event to HTMLImageElement before accessing any IMG attribute (React/Typescript example):您可以在访问任何 IMG 属性之前将目标元素从事件转换为 HTMLImageElement(React/Typescript 示例):
onError={
(e: React.SyntheticEvent<HTMLImageElement, Event>) => (
(e.target as HTMLImageElement).src = DEFAULT_AVATAR
)
}
您需要将其声明为HTMLImageElement
,它具有src
属性。
Using casting like @whereDatApp.com suggested worked for me. 像@whereDatApp.com建议的使用铸造为我工作。 Using query selector like this:
像这样使用查询选择器:
querySelector("img[name='menubanner']")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.