在reactjs中设置文本输入占位符颜色
Set text input placeholder color in reactjs
在使用普通 CSS 时,如果你想设置占位符的样式,你可以使用这些 css 选择器:
::-webkit-input-placeholder {
color: red;
}
但我不知道如何在 React 内联样式中应用这些类型的样式。
您不能使用 ::-webkit-inline-placeholder
内联。
它是一个伪元素(很像 :hover
)只能在您的样式表中使用:
The non-standard proprietary ::-webkit-input-placeholder
pseudo-element represents the placeholder text of a form element.
相反,通过 className
属性 将 class 分配给 React 组件 并将样式应用于此 class.
您可以尝试使用 radium
var Radium = require('radium');
var React = require('react');
var color = require('color');
@Radium
class Button extends React.Component {
static propTypes = {
kind: React.PropTypes.oneOf(['primary', 'warning']).isRequired
};
render() {
// Radium extends the style attribute to accept an array. It will merge
// the styles in order. We use this feature here to apply the primary
// or warning styles depending on the value of the `kind` prop. Since its
// all just JavaScript, you can use whatever logic you want to decide which
// styles are applied (props, state, context, etc).
return (
<button
style={[
styles.base,
styles[this.props.kind]
]}>
{this.props.children}
</button>
);
}
}
// You can create your style objects dynamically or share them for
// every instance of the component.
var styles = {
base: {
color: '#fff',
// Adding interactive state couldn't be easier! Add a special key to your
// style object (:hover, :focus, :active, or @media) with the additional rules.
':hover': {
background: color('#0074d9').lighten(0.2).hexString()
},
'::-webkit-input-placeholder' {
color: red;
}
},
primary: {
background: '#0074D9'
},
warning: {
background: '#FF4136'
}
};
对我来说,我使用Radium's Style component。以下是您可以使用 ES6 语法执行的操作:
import React, { Component } from 'react'
import Radium, { Style } from 'radium'
class Form extends Component {
render() {
return (<div>
<Style scopeSelector='.myClass' rules={{
'::-webkit-input-placeholder': {
color: '#929498'
}}} />
<input className='myClass' type='text' placeholder='type here' />
</div>
}
}
export default Radium(Form)
不要使用 ::-webkit-inline-placeholder inline 和 Radium 作为一个占位符。
我建议去你的index.css
input.yourclassname::placeholder { /* Chrome, Firefox, Opera, Safari 10.1+ */
color: white;
opacity: 1; /* Firefox */
}
只需给您的 "input" 标签一个 "id" 或 "class" 并将您的 css 样式放在 src 内的 App.css 中。例如
//App.css or external stylesheet
#inputID::placeholder {
color: #ff0000;
opacity: 1;
}
//your jsx code
<input type="text" id="inputID" placeholder="Your text here" />
这确实对我有用。
我的做法是简单地根据值是否为空对整个<input />
组件应用不同的样式。不需要安装新的依赖项,也不需要使用样式表,这似乎是原始问题的重点。
var inputStyles = {
border: '1px solid #cbcbcb',
color: '#525252',
};
var placeholderStyles = {
...inputStyles,
color: '#999999',
};
<input
type="text"
placeholder="enter text here"
value={myValue}
style={myValue ? inputStyles : placeholderStyles}
/>
使用 Template Literals 即 Backward quotes(``)
添加伪元素。
const inputFieldStyle = `
.inputField::-webkit-input-placeholder{
color: red;
}`
使用 class
很重要,因此请确保在伪元素之前使用 class。
然后你可以在传递上面样式的地方使用标签,如下所示:
const reactFunctionalComponent = (props) => {
...
return(
<>
<style>
{inputFieldStyle}
</style>
...
</>
)
}
在使用普通 CSS 时,如果你想设置占位符的样式,你可以使用这些 css 选择器:
::-webkit-input-placeholder {
color: red;
}
但我不知道如何在 React 内联样式中应用这些类型的样式。
您不能使用 ::-webkit-inline-placeholder
内联。
它是一个伪元素(很像 :hover
)只能在您的样式表中使用:
The non-standard proprietary
::-webkit-input-placeholder
pseudo-element represents the placeholder text of a form element.
相反,通过 className
属性 将 class 分配给 React 组件 并将样式应用于此 class.
您可以尝试使用 radium
var Radium = require('radium');
var React = require('react');
var color = require('color');
@Radium
class Button extends React.Component {
static propTypes = {
kind: React.PropTypes.oneOf(['primary', 'warning']).isRequired
};
render() {
// Radium extends the style attribute to accept an array. It will merge
// the styles in order. We use this feature here to apply the primary
// or warning styles depending on the value of the `kind` prop. Since its
// all just JavaScript, you can use whatever logic you want to decide which
// styles are applied (props, state, context, etc).
return (
<button
style={[
styles.base,
styles[this.props.kind]
]}>
{this.props.children}
</button>
);
}
}
// You can create your style objects dynamically or share them for
// every instance of the component.
var styles = {
base: {
color: '#fff',
// Adding interactive state couldn't be easier! Add a special key to your
// style object (:hover, :focus, :active, or @media) with the additional rules.
':hover': {
background: color('#0074d9').lighten(0.2).hexString()
},
'::-webkit-input-placeholder' {
color: red;
}
},
primary: {
background: '#0074D9'
},
warning: {
background: '#FF4136'
}
};
对我来说,我使用Radium's Style component。以下是您可以使用 ES6 语法执行的操作:
import React, { Component } from 'react'
import Radium, { Style } from 'radium'
class Form extends Component {
render() {
return (<div>
<Style scopeSelector='.myClass' rules={{
'::-webkit-input-placeholder': {
color: '#929498'
}}} />
<input className='myClass' type='text' placeholder='type here' />
</div>
}
}
export default Radium(Form)
不要使用 ::-webkit-inline-placeholder inline 和 Radium 作为一个占位符。
我建议去你的index.css
input.yourclassname::placeholder { /* Chrome, Firefox, Opera, Safari 10.1+ */
color: white;
opacity: 1; /* Firefox */
}
只需给您的 "input" 标签一个 "id" 或 "class" 并将您的 css 样式放在 src 内的 App.css 中。例如
//App.css or external stylesheet
#inputID::placeholder {
color: #ff0000;
opacity: 1;
}
//your jsx code
<input type="text" id="inputID" placeholder="Your text here" />
这确实对我有用。
我的做法是简单地根据值是否为空对整个<input />
组件应用不同的样式。不需要安装新的依赖项,也不需要使用样式表,这似乎是原始问题的重点。
var inputStyles = {
border: '1px solid #cbcbcb',
color: '#525252',
};
var placeholderStyles = {
...inputStyles,
color: '#999999',
};
<input
type="text"
placeholder="enter text here"
value={myValue}
style={myValue ? inputStyles : placeholderStyles}
/>
使用 Template Literals 即 Backward quotes(``)
添加伪元素。
const inputFieldStyle = `
.inputField::-webkit-input-placeholder{
color: red;
}`
使用 class
很重要,因此请确保在伪元素之前使用 class。
然后你可以在传递上面样式的地方使用标签,如下所示:
const reactFunctionalComponent = (props) => {
...
return(
<>
<style>
{inputFieldStyle}
</style>
...
</>
)
}