由于某些原因,当 React 更新输入值时,该值显示在 DOM 中。
目前我正在使用这段代码:
< input type="password" id="password" value="anytext" />
出于安全原因,是否有任何方法可以隐藏 value 属性中的文本?也许是这样的:
< input type="password" id="password" value />
非常感谢大家的意见。
最佳答案
在我看来这种行为不会发生:
class Demo extends React.Component {
constructor(props) {
super(props);
this.state = { password: 'test', test: '' }
setInterval(function() {
document.getElementById('test').innerText = 'ATTRIBUTE: ' + document.getElementById('password').getAttribute('value');
}.bind(this), 200);
}
render() {
return <div><input id="password" type="password" value={this.state.password} onChange={(e) => this.setState({password: e.target.value})} /><pre id="test">ATTRIBUTE: {this.state.test}</pre><pre id="value">VALUE: {this.state.password}</pre></div>
}
}
ReactDOM.render(
<Demo />,
document.getElementById("root")
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="root"/>
关于javascript - 从 dom 中隐藏输入值属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51562754/