gpt4 book ai didi

reactjs - 如何根据内容更改文本区域的高度?

转载 作者:行者123 更新时间:2023-12-03 13:58:08 25 4
gpt4 key购买 nike

我正在尝试根据内容高度更改文本区域的高度。我看到事件处理程序不会更改高度,因为它被引导样式覆盖。请帮忙!

class PostForm extends React.Component {
constructor(props){
super(props);
this.state = {titleHeight: '30', storyHeight: 1};
this.handleKeyUp = this.handleKeyUp.bind(this);
}
handleKeyUp(event){
this.setState({titleHeight: document.getElementById('post_title').scrollHeight});
this.setState({storyHeight: document.getElementById('post_story').scrollHeight});
}
render () {
var csrfToken = $('meta[name=csrf-token]').attr('content');
return (
<form action='create' method='post' acceptCharset='UTF-8' className= "form-group">
<input type='hidden' name='_method' value='patch'/>
<input type='hidden' name='utf8' value='✓' />
<input type='hidden' name='authenticity_token' value={csrfToken} />
<textarea id="post_title" name="post[title]" className="form-control boldText" style={formStyle.textArea} height={this.state.titleHeight} onKeyUp={this.handleKeyUp} placeholder="Title"/>
<textarea id="post_story" name="post[story]" className="form-control" style={formStyle.textArea} height={this.state.storyHeight} onKeyUp={this.handleKeyUp} placeholder="Start telling the story"/>
<input name="commit" type="submit" value="Post" className="btn" style={formStyle.button}/>
</form>
);
}
}

const formStyle = {
textArea: {
border: 5,
boxShadow: 'none',
margin: 5,
overflow: 'hidden',
resize: 'none',
ariaHidden: 'true',
},
button: {
backgroundColor: 'black',
color: 'white',
width: 70,
marginLeft: 18,
marginRight: 5,
},
}

最佳答案

textarea HTML component没有属性 height但是一个属性rows您可以将其用于此目的(例如 <textarea rows={Math.round(this.state.storyHeight)} ... /> )。

没有 CSS 样式会覆盖您在 style 中传递的内容属性,它的工作方式相反。但没有height在你的formStyle无论如何定义。

关于reactjs - 如何根据内容更改文本区域的高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46828441/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com