简体   繁体   English

错误:类型“ HTMLDivElement”上不存在属性“值”? 打字稿找不到我的div?

[英]ERROR: Property 'value' does not exist on type 'HTMLDivElement' ? Typescript can't find my div?

Learning Typescript and trying to build a simple textbox that displays the text once submitted. 学习打字稿并尝试构建一个简单的文本框,以显示提交后的文本。

Am able to capture the input great and and now trying to report it back to the user. 能够很好地捕获输入,现在尝试将其报告给用户。

The problem is on compilation typescript gives this error message: Property 'value' does not exist on type 'HTMLDivElement'. 问题出在编译打字稿上,并显示以下错误消息:类型“ HTMLDivElement”上不存在属性“值”。

Am simply trying to assign the value of the div messageList to print the array (don't care about formatting yet). 我只是试图分配div messageList的值来打印数组(现在还不关心格式)。

I have been searching the web and most I can find says it must be naming error, i tried changing the name to 1 and still no joy. 我一直在网上搜索,大多数我能找到的提示它一定是命名错误,我试图将名称更改为1,但仍然不满意。

Have attempted to use general HTML element in the cast but no joy. 尝试在演员表中使用常规HTML元素,但没有任何乐趣。

The TS code: TS代码:

let messages = [];

class Message {
  content: string;
  constructor(
    public message: string
  ){
    this.message = message
  }
}

function post(message: string){
  var text = (<HTMLInputElement>document.getElementById("messageInput")).value;
  const newPost = new Message(text)
  messages.push(newPost.message)
  console.log(messages)
  this.report(newPost)
}

function report(message: Message){
  (<HTMLDivElement>document.getElementById("messageList")).value = messages
}

the HTML: HTML:

<!DOCTYPE html>
<html>
<head>
  <title>Typescript Messages</title>
</head>
  <body>
    <textarea id="messageInput" type="text" name="message" rows="15" cols="40"></textarea>
    <button id="submitButton" type="submit" value="submit" onclick="post()">Post</button>
    <div id="messageList" value="">Hello rubhy</div>
    <script src="message.js"></script>
  </body>
</html>

Any help would be appreciated, thank you 任何帮助,将不胜感激,谢谢

function report(message: Message){
  (<HTMLDivElement>document.getElementById("messageList")).value = messages
}

You need to cast it as HTMLInputElement as you done with text in post function: 您需要像在post函数中处理text一样将其强制转换为HTMLInputElement:

function report(message: Message){
  (<HTMLInputElement>document.getElementById("messageList")).value = messages
}

The HTMLDivElement type doesn't have the value property, so that's why you get that error. HTMLDivElement类型不具有value属性,因此这就是您收到该错误的原因。

EDIT: I just saw that messageList is a <div> . 编辑:我刚刚看到messageList是<div> Div cannot contain a value property. Div不能包含value属性。 For custom properties, use data-* 对于自定义属性,请使用data-*

https://www.w3schools.com/tags/att_data-.asp https://www.w3schools.com/tags/att_data-.asp

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 与 Typescript 反应:“DetailedHTMLProps”类型上不存在属性“名称” <HTMLAttributes<HTMLDivElement> , HTMLDivElement&gt;&#39; - React with Typescript: Property 'name' does not exist on type 'DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>' Typescript 错误“类型‘HTMLElement’上不存在属性‘值’”、“‘元素’类型上不存在属性‘onclick’” - Typescript error "Property 'value' does not exist on type 'HTMLElement'", "Property 'onclick' does not exist on type 'Element'" 打字稿中的错误~类型void~中不存在属性 - Error in typescript ˜Property does not exist in type void˜ Ionic TypeScript错误(类型“ HomePage”上不存在属性“ nav”) - Ionic TypeScript Error (Property 'nav' does not exist on type 'HomePage') 打字稿错误:类型“元素”上不存在属性“包含”。 - Typescript error :Property 'contains' does not exist on type 'Element'.? 打字稿错误“ HTMLElement”类型不存在属性。 任何 - Typescript error Property does not exist on type 'HTMLElement'. any Typescript:“EventTarget”类型上不存在属性“className” - Typescript :Property 'className' does not exist on type 'EventTarget' 类型“#”错误中不存在属性“#” - Property "#" does not exist on type "#" error TypeScript static 方法中的属性不存在错误 - Property does not exist error in TypeScript static method 类型错误时不存在 Angular 属性 - Angular Property does not exist on type error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM