简体   繁体   English

Typescript:“HTMLElement”类型上不存在属性“src”

[英]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使用setAttributesrc设置为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.

相关问题 Typescript 中的“HTMLElement”类型上不存在属性“内容” - Property 'content' does not exist on type 'HTMLElement' in Typescript useRef Typescript 错误:“HTMLElement”类型上不存在属性“current” - useRef Typescript error: Property 'current' does not exist on type 'HTMLElement' 打字稿错误:&#39;HTMLElement&#39;类型中不存在属性&#39;append&#39; - Typescript Error: Property 'append' does not exist on type 'HTMLElement' Typescript 错误“类型‘HTMLElement’上不存在属性‘值’”、“‘元素’类型上不存在属性‘onclick’” - Typescript error "Property 'value' does not exist on type 'HTMLElement'", "Property 'onclick' does not exist on type 'Element'" Typescript:属性“数据”在HTMLElement类型上不存在 - Typescript : Property 'data' dose not exist on type HTMLElement 类型“JQuery”上不存在属性“slick”<HTMLElement> &#39; - Property 'slick' does not exist on type 'JQuery<HTMLElement>' 类型“ HTMLElement”上不存在属性“ length” - Property 'length' does not exist on type 'HTMLElement' 错误:类型 HTMLElement 上不存在属性“选择” - error:Property 'select' does not exist on type HTMLElement “HTMLElement”类型上不存在属性“r” - Property 'r' does not exist on type 'HTMLElement' 类型“JQuery”上不存在属性“模态”<HTMLElement> &#39; - Property 'modal' does not exist on type 'JQuery<HTMLElement>'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM