简体   繁体   English

为什么我不能用 getElementById().value 设置文本?

[英]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.

相关问题 为什么不能设置`document.getElementById(“ text”); `要全球化? - Why can't set `document.getElementById(“text”); ` to be global? 当字段没有ID但有名称时,如何设置文本输入字段的值。 我无法使用document.getElementById - How do I set the value of a text input field when the field does not have an id but it has a name. I can't use document.getElementById 为什么我的if语句不能以document.getElementById(“ ID”)。value = var结尾? - Why can't I end my if statement with document.getElementById(“ID”).value = var? JavaScript &gt; 如果我设置“var x = document.getElementById('inputText').value”并更新“x”,为什么值没有更新? - JavaScript > If I set “var x = document.getElementById('inputText').value” and I update “x”, why doesn't the value update? GetElementById无法获得价值 - GetElementById can't get value 为什么我无法使用GetElementById在页面上找到元素? - Why can't I find elements on a page using GetElementById? 为什么我不能在箭头函数中省略getElementById()的参数? - Why can't I omit the parameter of getElementById() in the Arrow Function? 为什么不能多次使用具有相同ID的getElementById? - Why can't I use getElementById multiple times with the same id? 为什么在使用 getElementById 时无法更改显示样式? - Why can't I change display style when using getElementById? 为什么我不能在javascript元素上调用getElementById? - Why can't I call getElementById on a javascript element?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM