简体   繁体   English

“.value”是输入元素的属性吗?

[英]is “.value” an attribute of input element?

return (
      <input 
        type="text" 
        placeholder="Type a color"
        onChange={(e)=> {
          changeColor(e.target.value)
        }}
        ></input>
  )

While I was studying React, I saw this code above, and I could not fully understand why I am able to display value typed into the input element.在学习 React 时,我看到了上面的这段代码,我无法完全理解为什么我能够显示输入到输入元素中的值。 (I didn't put HTML file this time). (这次我没有放HTML文件)。 I know "e" is an event and by using target property, it's referencing the object, but what is ".value" in this case?我知道“e”是一个事件,通过使用目标属性,它引用了 object,但在这种情况下,“.value”是什么? I saw one website says it is HTML DOM property, which specifies the value of the attribute, but is it HTML DOM property for input element or something else?我看到一个网站说它是 HTML DOM 属性,它指定了属性的值,但它是输入元素的 HTML DOM 属性还是其他什么? I appreciate your help!我感谢您的帮助!

By default, HTML input element has a value attribute.默认情况下,HTML input元素具有value属性。 You can set it to have a default text appear in the input on page load.您可以将其设置为在页面加载时在输入中显示默认文本。

You correctly mentioned e is an event object.您正确地提到了e是一个事件 object。 It has an attribute target which is the element that created this event.它有一个属性target ,它是创建此事件的元素。 So in your case e.target.value refers correctly to the value of the input element whos value changed.因此,在您的情况下, e.target.value正确地指的是值已更改的input元素的值。

value is an attribute for input tag as seen from here in W3 Documentation . valueinput标签的属性,如W3 文档中的此处所示。

You are correct that e is an event, and by using target property, you are able to determine the element that caused the event.您是正确的e是一个事件,并且通过使用target属性,您可以确定导致该事件的元素。

Further reading about this can be seen in FreeCodeCamp .可以在FreeCodeCamp中看到有关此的进一步阅读。 I recommend doing so, and you will see the most common use of this practice.我建议这样做,你会看到这种做法最常见的用法。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM