[英]Why I can't set the text with getElementById().value?
<div className="mb-3 form-check">
<input type="checkbox" className="form-check-input" id="exampleCheck1" />
<label className="form-check-label" htmlFor="exampleCheck1" id="suca">
Accetto di vedere peni
</label>
</div>
<button type="submit" className="btn btn-primary" onClick={(event)=>
{document.getElementById("suca").value="lol";
props.onVamo(document.getElementById("Username").value,document.getElementById("NumeroStanza").value,event)}}>
Vamo
</button>
Why document.getElementById("suca").value="lol";
为什么document.getElementById("suca").value="lol";
why doesn't this actually set the text to "lol"?为什么这实际上没有将文本设置为“lol”? I don't get it..我不明白。。
Besides "the react way" considerations, value
is a valid atteibute for inputs/textareas, labels and other html elements have innerHTML
除了“反应方式”考虑之外, value
是输入/文本区域、标签和其他 html 元素的有效属性具有innerHTML
document.getElementById("blyat").innerHTML = "I have changed;"; document.getElementById("blyat").innerHTML = "我变了;";
As Konrad mentioned its very dangerous to change your HTML in a reactive environment:正如 Konrad 提到的,在反应性环境中更改 HTML 非常危险:
React.js: Set innerHTML vs dangerouslySetInnerHTML React.js:设置 innerHTML 与危险地SetInnerHTML
You should set this elements value to a variable, and change the variable:)您应该将此元素值设置为变量,并更改变量:)
I also suggest using dangerouslySetInnerHTML
:我还建议使用dangerouslySetInnerHTML
:
dangerouslySetInnerHTML
is a property that you can use on HTML elements in a React application to programmatically set their content. dangerouslySetInnerHTML
是一个属性,您可以在 React 应用程序中的 HTML 元素上使用它以编程方式设置其内容。 Instead of using a selector to grab the HTML element, then setting its innerHTML, you can use this property directly on the element.您可以直接在元素上使用此属性,而不是使用选择器来抓取 HTML 元素,然后设置其 innerHTML。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.