简体   繁体   English

编写 JS 存储 10 个值但 TypeError: input is a void element tag and must not have `children` 也不能使用 `dangerouslySetInnerHTML

[英]Writing JS to Store 10 Values But TypeError: input is a void element tag and must neither have `children` nor use `dangerouslySetInnerHTML

i Am Writing A JavaScript Function In React For Adding 10 Values To Array This 10 Values To Be Stored From Html Input Field So what i did is added input field with id 'tsk ' and button 'bt3' so whenever the button get hit it run a js script which takes the value of input field 'tsk' and pushed it to empty array in js so every time i hit button the value inside field is pushed to array and prompt over label 'list ' by using innerHTML method i Am Writing A JavaScript Function In React For Adding 10 Values To Array This 10 Values To Be Stored From Html Input Field So what i did is added input field with id 'tsk ' and button 'bt3' so whenever the button get hit it run一个 js 脚本,它获取输入字段 'tsk' 的值并将其推送到 js 中的空数组,因此每次我点击按钮时,字段内的值都会被推送到数组并通过使用 innerHTML 方法提示 label 'list'

here my efforts of html is这里我的 html 的努力是

 <div id='array'>
      <input type='number' id='tsk'>push 10 values</input>
      <label htmlFor='list' id='list' >List values are:</label>
      <button id='bt3' onClick={tsk3}>ADD Value</button>
    </div>

and here is corresponding js to run it这是相应的js来运行它

function tsk3()
{
  let item = document.getElementById('tsk').value;
  var array =[1,];
  array.push(item);
  document.getElementById('list').innerHTML=array;
}

but instead of working the code it showing me unnecessary type error OUTPUT OF WEBPAGE但它没有运行代码,而是向我显示了不必要的类型错误OUTPUT OF WEBPAGE

This error occurs because in React when void elements have either children or dangerouslySetInnerHTML prop.发生此错误是因为在 React 中,当 void 元素具有子元素或dangerouslySetInnerHTML的 SetInnerHTML 属性时。 HTML elements such as are void elements that are only self-closing without any content. HTML 元素,例如 void 元素,它们只会自动关闭而没有任何内容。 Therefore, React will throw an exception if you set either children or the innerHTML property for a void element.因此,如果您为 void 元素设置 children 或 innerHTML 属性,React 将抛出异常。 Also, there are syntax errors in your code.此外,您的代码中存在语法错误。 The htmlFor property sets or returns the value of the for attribute of a label. htmlFor 属性设置或返回 label 的 for 属性的值。 The htmlFor attribute specifies which form element a label is bound to which in your case should be 'tsk' ie the input element. htmlFor 属性指定 label 绑定到哪个表单元素,在您的情况下应该是“tsk”,即输入元素。 Accordingly, the label tag should appear before the input tag, something like the following -因此,label 标签应该出现在输入标签之前,如下所示 -

 <div id='array'> <label htmlFor='task' id='list' >List values are:</label> <input type='number' id='tsk' placeholder='push 10 values'/> <button id='bt3' onClick={tsk3}>ADD Value</button> </div>

Also note input has to be a self-closing tag.另请注意,输入必须是自闭合标签。

暂无
暂无

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

相关问题 未捕获的错误:输入是一个空元素标签,既不能有`children`也不能使用`dangerouslySetInnerHTML` - Uncaught Error: input is a void element tag and must neither have `children` nor use `dangerouslySetInnerHTML` ReactJs:输入是一个空元素标签,不能有`children`也不能使用`dangerouslySetInnerHTML` - ReactJs: input is a void element tag and must neither have `children` nor use `dangerouslySetInnerHTML` ReactJS 错误输入是一个空元素标签,不能有`children`也不能使用`dangerouslySetInnerHTML` - ReactJS Error input is a void element tag and must neither have `children` nor use `dangerouslySetInnerHTML` Reactjs 警告:输入是一个空元素标签,不能有 `children` 或使用 `props.dangerouslySetInnerHTML`。 检查null的render方法 - Reactjs Warning: input is a void element tag and must not have `children` or use `props.dangerouslySetInnerHTML`. Check the render method of null 我可以访问元素(输入标签),但不能访问 textContent 和 innerText 的内容 - i can access the element(input tag) but not its content for neither textContent nor innerText 声明类型既不是“ void”也不是“ any”的函数必须返回一个值。 TypeScript / React - A function whose declared type is neither 'void' nor 'any' must return a value. TypeScript/React angular 中的 TypeScript 错误:声明类型既不是“void”也不是“any”的 function 必须返回一个值 - TypeScript error in angular: A function whose declared type is neither 'void' nor 'any' must return a value 带有来自相机的输入的视频标签可在 iOS 上的 Safari 中运行,但既不能在 Chrome 中也不能在任何应用内浏览器中运行 - Video tag with input from Camera is Working in Safari on iOS but neither working in Chrome nor in any In-app Browser JavaScript 代码既不能在 HTML 代码的脚本标签内工作,也不能在外部 JS 文件中工作 - JavaScript code neither working within script tag in HTML code nor in an external JS file JS在asp文本框或输入中不显示值 - JS not displaying values in asp Textbox Nor Input
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM